黑马程序员技术交流社区
标题:
今天学的面向对象,手机注释版
[打印本页]
作者:
a554305211
时间:
2015-8-27 21:39
标题:
今天学的面向对象,手机注释版
class TestSuperPhone {
public static void main(String[] args){
Phone p = new Phone(); //创建一个Phone类的对象p,并为其在堆内存中分配空间
p.setBrand("苹果手机"); //调用Phone对象中的setBrand方法,为手机品牌brand赋值
p.setPrice(7777); //调用Phone对象中的setPrice方法,为手机价格price赋值
System.out.println(p.getBrand() + "价格为:" + p.getPrice());//打印对象p的手机品牌以及价格
p.call(); //调用Phone对象中的call方法
p.sendMessage(); //调用Phone对象中的sendMessage方法
p.playGame(); //调用Phone对象中的setGames方法
}
}
class Phone {
/*
手机类
属性:品牌brand,价格price
行为:打电话call,发短信sendMessage,玩游戏,playGame
*/
private String brand; //创建私有成员变量brand
private int price; //创建私有成员变量price
void setBrand(String brand){ //创建私有成员变量brank的set方法(设置brank的值)
this.brand = brand;
}
String getBrand(){ //创建私有成员变量brank的get方法(获取brank的值)
return brand;
}
void setPrice(int price){ //创建私有成员变量price的set方法(设置price的值)
this.price = price;
}
int getPrice(){ //创建私有成员变量price的set方法(设置price的值)
return price;
}
void call(){ //打电话的方法
System.out.println("打电话");
}
void sendMessage(){ //发短信的方法
System.out.println("发短信");
}
void playGame(){ //玩游戏的方法
System.out.println("玩游戏");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2