使用java语言设计一组类(至少3个类),分别表示计算机,台式机,笔记本,服务器(注意使用继承)
时间: 2024-05-06 13:18:24 浏览: 60
下面是使用Java语言设计的一组类,分别表示计算机,台式机,笔记本和服务器,其中台式机和笔记本都继承自计算机类,服务器则继承自台式机类。
```
public class Computer {
private String brand;
private String model;
private int price;
public Computer(String brand, String model, int price) {
this.brand = brand;
this.model = model;
this.price = price;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public int getPrice() {
return price;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setModel(String model) {
this.model = model;
}
public void setPrice(int price) {
this.price = price;
}
}
public class Desktop extends Computer {
private String cpu;
private int memorySize;
private int hardDiskSize;
public Desktop(String brand, String model, int price, String cpu, int memorySize, int hardDiskSize) {
super(brand, model, price);
this.cpu = cpu;
this.memorySize = memorySize;
this.hardDiskSize = hardDiskSize;
}
public String getCpu() {
return cpu;
}
public int getMemorySize() {
return memorySize;
}
public int getHardDiskSize() {
return hardDiskSize;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public void setMemorySize(int memorySize) {
this.memorySize = memorySize;
}
public void setHardDiskSize(int hardDiskSize) {
this.hardDiskSize = hardDiskSize;
}
}
public class Laptop extends Computer {
private String cpu;
private int memorySize;
private int hardDiskSize;
private int batteryCapacity;
public Laptop(String brand, String model, int price, String cpu, int memorySize, int hardDiskSize, int batteryCapacity) {
super(brand, model, price);
this.cpu = cpu;
this.memorySize = memorySize;
this.hardDiskSize = hardDiskSize;
this.batteryCapacity = batteryCapacity;
}
public String getCpu() {
return cpu;
}
public int getMemorySize() {
return memorySize;
}
public int getHardDiskSize() {
return hardDiskSize;
}
public int getBatteryCapacity() {
return batteryCapacity;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public void setMemorySize(int memorySize) {
this.memorySize = memorySize;
}
public void setHardDiskSize(int hardDiskSize) {
this.hardDiskSize = hardDiskSize;
}
public void setBatteryCapacity(int batteryCapacity) {
this.batteryCapacity = batteryCapacity;
}
}
public class Server extends Desktop {
private int numOfCPUs;
private int numOfHardDisks;
public Server(String brand, String model, int price, String cpu, int memorySize, int hardDiskSize, int numOfCPUs, int numOfHardDisks) {
super(brand, model, price, cpu, memorySize, hardDiskSize);
this.numOfCPUs = numOfCPUs;
this.numOfHardDisks = numOfHardDisks;
}
public int getNumOfCPUs() {
return numOfCPUs;
}
public int getNumOfHardDisks() {
return numOfHardDisks;
}
public void setNumOfCPUs(int numOfCPUs) {
this.numOfCPUs = numOfCPUs;
}
public void setNumOfHardDisks(int numOfHardDisks) {
this.numOfHardDisks = numOfHardDisks;
}
}
```
阅读全文