定义一个类,用来模拟“手机”事物。
属性:品牌、价格、颜色
行为:打电话、发短信
对应到类当中;
成员变量(属性):
String brand; //品牌
double price; //价格
String color; //颜色
成员方法(行为):
public void call(String who){}打电话
public static void sendMessage(){}发信息
String brand; double price; String color; public void call(String who){ System.out.println("给" + who + "打电话"); } public static void sendMessage(){ System.out.println("群发信息"); }
Phone phone = new Phone(); System.out.println(phone.brand); System.out.println(phone.price); System.out.println(phone.color); System.out.println("======="); phone.brand = "苹果"; phone.price = 8388.0; phone.color = "黑色"; System.out.println(phone.brand); System.out.println(phone.price); System.out.println(phone.color); System.out.println("======="); phone.call("钱伯斯"); phone.sendMessage();