【案例3-3】多功能手机
【案例介绍】
随着科技的发展,手机的使用已经普及到每个家庭甚至个人,手机的属性越来越强大,功能也越来越多,因此人们在生活中越来越依赖于手机。
任务要求,使用所学知识编写一个手机属性及功能分析程序设计,测试各个手机的属性及功能。使用手机时,输出当前手机的各个属性参数以及正在使用的功能。
【代码】
/* Phone.java */
package com.j2se.myInstances.example3_3;
public class Phone {
private String brand;
private String type;
private String os;
private double price;
private int memory;
public Phone() {}
public Phone(String brand, String type, String os, double price, int memory) {
this.brand = brand;
this.type = type;
this.os = os;
this.price = price;
this.memory = memory;
}
public void about() {
/* 查看手机信息 */
System.out.println("Brand: " + brand);
System.out.println("Type: " + type);
System.out.println("OS: " + os);
System.out.println("Price(RMB): " + price);
System.out.println("Memory(G): " + memory);
}
public void call(String number) {
System.out.println("=>自动拨号");
String phoneNumber = null;
switch (number) {
case "1":
phoneNumber = "打电话给詹姆斯...";
break;
case "2":
phoneNumber = "打电话给杜兰特...";
break;
case "3":
phoneNumber = "打电话给库日天";
break;
}
System.out.println(phoneNumber);
}
public void playGame() {
System.out.println("玩星球大战");
}
public void downloadMusic() {
System.out.println("Downloading...");
System.out.println("Finished...");
}
public void playMusic(String song) {
System.out.println("正在播放:" + song);
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
}
/* PhoneTest.java */
package com.j2se.myInstances.example3_3;
public class PhoneTest {
public static void main(String[] args) {
Phone p1 = new Phone();
p1.setBrand("Apple");
p1.setType("iPhone Xs");
p1.setOs("ios");
p1.setMemory(256);
p1.setPrice(8999);
p1.about();
p1.call("1");
p1.playMusic("Hey Judy");
p1.playGame();
System.out.println("======================");
Phone p2 = new Phone("Huawei", "Huawei P50", "HarmonyOs", 10099, 128);
p2.about();
p2.call("3");
p2.playGame();
p2.playMusic("好运来");
}
}