创建一个电脑类Computer类。
类中有变量品牌brand 、内存memory、型号 model
定义类的无参构造方法
定义为品牌赋值的构造方法
定义为内存和型号赋值的构造方法
设置内存的方法,要求内存必须是8、16、32之一
获取内存的方法
返回类的品牌信息的方法
创建一个对象obj,品牌为“联想”,型号为“天逸“,内存为”16“G
并把该电脑配置输出到屏幕
希望对您有所帮助,望采纳
public class Computer {
private String brand;
private String memory;
private String model;
public Computer() {
}
public Computer(String brand) {
this.brand = brand;
}
public Computer(String memory, String model) {
this.memory = memory;
this.model = model;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getMemory() {
return memory;
}
public void setMemory(String memory) {
if ("8".equals(memory) || "16".equals(memory) || "32".equals(memory)) {
this.memory = memory;
} else {
System.out.println("内存必须为8或16或32");
}
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String toString() {
return "电脑信息:品牌为【" + this.brand + "】," +
"型号为【" + this.model + "】,内存为【" + this.memory + "】G";
}
}
public class Main {
public static void main(String[] args) {
Computer computer = new Computer("联想");
computer.setMemory("16");
computer.setModel("天逸");
System.out.println(computer.toString());
}
}