定义类:
//定义一个手机类
//属性:创建品牌、颜色、价格
//行为:给xxx打电话
群发短信
public class phone {
String brand;
String color;
int price;
public void call(String name){
System.out.println("给"+ name +"打电话");
}
public void sendMessage(){
System.out.println("输出群发短信");
}
}
//编写一个手机测试类,创建两个对象并赋值
public class phoneTest {
public static void main(String[] args) {
phone ph1=new phone();
ph1.brand="小米";
ph1.color="白色";
ph1.price=4999;
System.out.println(ph1.brand);
System.out.println(ph1.color);
System.out.println(ph1.price);
ph1.call("张三");
ph1.sendMessage();
System.out.println("------------");
phone ph2=new phone();
ph2.brand="华为";
ph2.color="黑色";
ph2.price=6999;
System.out.println(ph2.brand);
System.out.println(ph2.color);
System.out.println(ph2.price);
ph2.call("李四");
ph2.sendMessage();
}
}
标签:定义,对象,创建,System,color,println,ph2,ph1,out
From: https://blog.51cto.com/u_15988249/6222017