Chinaunix首页 | 论坛 | 博客
  • 博客访问: 686285
  • 博文数量: 152
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1793
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 12:26
个人简介

相信自己,只有不想做的,没有做不到的。

文章分类

全部博文(152)

文章存档

2021年(1)

2015年(2)

2014年(74)

2013年(75)

分类: Java

2014-08-09 06:16:23

实验1:定义一个“手机”类

目标:掌握通过类来创建对象的方法,以及对对象尽心相关的操作。,使用package的用法



在工程选择new->java project ->next->finish;

源文件 MobileTest.java  默认包名为 defluat package
import Mobile.Mobile;  //把Mobile的这个包加入到这个文件中,相当于C语言中的include

public class MobileTest {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


String b;
String t;
int w;

Mobile mobile = new Mobile();
mobile.setBrand("Apple");
mobile.setType("4s");
mobile.setWeight(500);
mobile.setMobile("Apple", "4s", 200);


System.out.println("手机品牌:" + mobile.getBrand());
System.out.println("手机类型:" + mobile.getType());
System.out.println("手机重量:" + mobile.getWeight()+"g");

b = mobile.getBrand();
t = mobile.getType();
w = mobile.getWeight();

System.out.println("手机品牌:" + b);
System.out.println("手机类型:" + t);
System.out.println("手机重量:" + w);


}


}



源文件 Mobile.java    创建一个package 包名为Mobile

package Mobile;

public class Mobile {
String Brand;
String type;
int weight;

//构造
public void setMobile(String b,String t,int w){
Brand = b;
type = t;
weight = w;
}

//定义属性,brand 的设置方法
public void setBrand (String _Brand){
Brand = _Brand;
}

//获得方法
public String getBrand (){

return Brand;
}

//type 设置方法
public void setType(String _type){
type = _type;
}


//获得 type
public String getType(){

return type;
}

//weight 设置方法
public void setWeight(int _weight){
weight = _weight;
}

//获得 weight
public int  getWeight(){

return weight;
}

}




就这样完成了pakeage的使用,构造器的使用方法,程序面对对象的思想。




阅读(407) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~