任务要求,使用所学知识编写一个手机属性及功能分析程序设计,测试各个手机的属性及功能。使用手机时,输出当前手机的各个属性参数以及正在使用的功能。
创建手机类
package 实验四;
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);
System.out.println("型号: " + type);
System.out.println("操作系统: " + os);
System.out.println("价格(RMB): " + price);
System.out.println("内存(G): " + memory);
}
public void call(String number) {
System.out.println("使用自动拨号功能");
String phoneNumber = null;
switch (number) {
case "1":
phoneNumber = "爷爷的号";
break;
case "2":
phoneNumber = "奶奶的号";
break;
}
System.out.println(phoneNumber);
}
public void playGame() {
System.out.println("玩扫雷游戏");
}
public void downloadMusic() {
System.out.println("开始下载");
System.out.println("下载完成");
}
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;
}
}
创建测试类
package 实验四;
public class phonetest {
public static void main(String[] args) {
phone p1 = new phone();
p1.setBrand("苹果");
p1.setType("iPhone Xs");
p1.setOs("ios");
p1.setPrice(8888);
p1.setMemory(16);
p1.about();
p1.call("1");
p1.playMusic("我的中国心");
p1.playGame();
System.out.println("*********************");
phone p2 = new phone("华为", "华为荣耀20", "Android", 6666, 16);
p2.about();
p2.call("3");
p2.playGame();
p2.playMusic("北京欢迎你");
}
}