标签:price demo one ice mes who pre his 运行
/**定义一个类,用来模拟“手机”事物。
属性:品牌、价格、颜色
行为:打电话、发短信
对应到类当中:
成员变量(属性):
String brand;//品牌
double price;//价格
String color;//颜色
成员方法(行为):
public void call(String who){} //打电话
public void sendMessage() {} //群发短信
*/
public class Demo02Phone {
//成员变量(属性):
String brand;//品牌
double price;//价格
String color;//颜色
public Demo02Phone() {
super();
}
public Demo02Phone(String brand, double price, String color) {
super();
this.brand = brand;
this.price = price;
this.color = color;
}
//成员方法(行为):
public void call(String who){
System.out.println("给"+who+"打电话");
}
public void sendMessage() {
System.out.println("群发信息");
}
@Override
public String toString() {
return "品牌:" + brand + " 价格:" + price + " 颜色:" + color ;
}
}
public class Demo02PhoneOne {
public static void main(String[] args) {
//根据Phone类,创建一个名为one的对象
//格式: 类名称 对象名 = new 类名称();
Demo02Phone one = new Demo02Phone();
System.out.println("品牌:"+one.brand+" 价格:"+one.price+" 颜色:"+one.color);
//直接给成员赋值
one.brand = "苹果";
one.price = 22200;
one.color = "red";
System.out.println("品牌:"+one.brand+" 价格:"+one.price+" 颜色:"+one.color);
//调用构造函数 赋值
Demo02Phone one2 = new Demo02Phone("华为",4000,"blue");
System.out.println(one2.toString());
one2.call("xiaobai");
one2.sendMessage();
}
}
品牌:null 价格:0.0 颜色:null
品牌:苹果 价格:22200.0 颜色:red
品牌:华为 价格:4000.0 颜色:blue
给xiaobai打电话
群发信息
标签:price demo one ice mes who pre his 运行
原文地址:https://www.cnblogs.com/Shuangyi/p/10852347.html