用java设计一组类,分别表示计算机,台式机,笔记本,服务器
时间: 2023-11-22 21:04:14 浏览: 133
好的,为您设计一组 Java 类:
```
public class Computer {
private String brand;
private String model;
public Computer(String brand, String model) {
this.brand = brand;
this.model = model;
}
public void turnOn() {
System.out.println("Computer is turning on...");
}
public void turnOff() {
System.out.println("Computer is turning off...");
}
}
public class Desktop extends Computer {
private String monitor;
public Desktop(String brand, String model, String monitor) {
super(brand, model);
this.monitor = monitor;
}
public void playGame() {
System.out.println("Playing game on desktop...");
}
}
public class Laptop extends Computer {
private double weight;
public Laptop(String brand, String model, double weight) {
super(brand, model);
this.weight = weight;
}
public void carryAround() {
System.out.println("Carrying laptop around...");
}
}
public class Server extends Computer {
private boolean rackMounted;
public Server(String brand, String model, boolean rackMounted) {
super(brand, model);
this.rackMounted = rackMounted;
}
public void processRequests() {
System.out.println("Processing requests on server...");
}
}
```
以上是一个简单的类设计,其中 `Computer` 是基类,其他三个类都继承自它。`Desktop`、`Laptop`、`Server` 分别表示台式机、笔记本和服务器,它们都有一些特殊的属性和方法。您可以在此基础上进一步扩展和优化。
阅读全文