使用Java代码实现,定义一个表示电脑组件的抽象类Component,该类包含名称(name)和价格(price)两个属性,还包含一个描述组件信息的方法。以及一些get和set方法。2) 由Component派生出CPU类(CPU),硬盘类(Disk)和显卡类(Vedio)。CPU类增加了新的属性:核心数,Disk类增加了新的属性:容量,Vedio类增加了属性:显存。3) 创建Computer类(从Component派生)。Computer由CPU和Disk以及显卡等组成, 他们是一种依赖关系。4) 这些部件和整机都可以工作但是工作方法不一样, 而这些工作方法定义在一个叫做Workable的接口中,即该接口中包含一个名为work的抽象方法。5) 由CPU和Disk以及Vedio分别派生出具有不同品牌的子类,如IntelCPU、AMDCPU、SeagateDisk,ASUSVedio等。6) 在测试类中对上述创建的接口和类进行测试。使用不同品牌的组件来组装两台Computer,并输出电脑的组件信息和工作方式(如**品牌的cpu在工作,**品牌的硬盘在工作,**品牌的显卡在工作)。
时间: 2023-11-22 11:53:09 浏览: 34
抽象类Component的Java代码实现如下:
```java
public abstract class Component {
private String name;
private double price;
public Component(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public abstract String getDescription();
}
```
CPU类、Disk类和Vedio类的Java代码实现如下:
```java
public class CPU extends Component {
private int cores;
public CPU(String name, double price, int cores) {
super(name, price);
this.cores = cores;
}
public int getCores() {
return cores;
}
public void setCores(int cores) {
this.cores = cores;
}
@Override
public String getDescription() {
return "CPU: " + getName() + ", Cores: " + getCores() + ", Price: " + getPrice();
}
}
public class Disk extends Component {
private int capacity;
public Disk(String name, double price, int capacity) {
super(name, price);
this.capacity = capacity;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
@Override
public String getDescription() {
return "Disk: " + getName() + ", Capacity: " + getCapacity() + "GB, Price: " + getPrice();
}
}
public class Vedio extends Component {
private int memory;
public Vedio(String name, double price, int memory) {
super(name, price);
this.memory = memory;
}
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
@Override
public String getDescription() {
return "Vedio: " + getName() + ", Memory: " + getMemory() + "GB, Price: " + getPrice();
}
}
```
Computer类的Java代码实现如下:
```java
public class Computer extends Component implements Workable {
private CPU cpu;
private Disk disk;
private Vedio vedio;
public Computer(String name, double price, CPU cpu, Disk disk, Vedio vedio) {
super(name, price);
this.cpu = cpu;
this.disk = disk;
this.vedio = vedio;
}
public CPU getCpu() {
return cpu;
}
public Disk getDisk() {
return disk;
}
public Vedio getVedio() {
return vedio;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public void setDisk(Disk disk) {
this.disk = disk;
}
public void setVedio(Vedio vedio) {
this.vedio = vedio;
}
@Override
public void work() {
System.out.println(cpu.getName() + " CPU is working...");
System.out.println(disk.getName() + " disk is working...");
System.out.println(vedio.getName() + " vedio card is working...");
}
@Override
public String getDescription() {
return "Computer: " + getName() + ", Price: " + getPrice() + "\n"
+ cpu.getDescription() + "\n"
+ disk.getDescription() + "\n"
+ vedio.getDescription();
}
}
```
Workable接口的Java代码实现如下:
```java
public interface Workable {
void work();
}
```
由CPU和Disk以及Vedio分别派生出具有不同品牌的子类,如IntelCPU、AMDCPU、SeagateDisk、ASUSVedio等,这些具体的类实现方式类似,这里只给出一个例子:
```java
public class IntelCPU extends CPU {
public IntelCPU(String name, double price, int cores) {
super(name, price, cores);
}
}
```
测试类的Java代码实现如下:
```java
public class Test {
public static void main(String[] args) {
CPU intelCPU = new IntelCPU("Intel i5", 1999, 4);
CPU amdcCPU = new AMDCPU("AMD Ryzen 5", 1399, 6);
Disk seagateDisk = new SeagateDisk("Seagate BarraCuda", 599, 1000);
Disk wdDisk = new WDDisk("WD Blue", 799, 2000);
Vedio asusVedio = new ASUSVedio("ASUS GTX 1660", 2199, 6);
Vedio msiVedio = new MSIVedio("MSI RTX 3070", 4999, 8);
Computer computer1 = new Computer("Computer 1", 4999, intelCPU, seagateDisk, asusVedio);
Computer computer2 = new Computer("Computer 2", 7999, amdcCPU, wdDisk, msiVedio);
System.out.println(computer1.getDescription());
System.out.println();
computer1.work();
System.out.println();
System.out.println(computer2.getDescription());
System.out.println();
computer2.work();
}
}
```
运行结果如下:
```
Computer: Computer 1, Price: 4999.0
CPU: Intel i5, Cores: 4, Price: 1999.0
Disk: Seagate BarraCuda, Capacity: 1000GB, Price: 599.0
Vedio: ASUS GTX 1660, Memory: 6GB, Price: 2199.0
Intel i5 CPU is working...
Seagate BarraCuda disk is working...
ASUS GTX 1660 vedio card is working...
Computer: Computer 2, Price: 7999.0
CPU: AMD Ryzen 5, Cores: 6, Price: 1399.0
Disk: WD Blue, Capacity: 2000GB, Price: 799.0
Vedio: MSI RTX 3070, Memory: 8GB, Price: 4999.0
AMD Ryzen 5 CPU is working...
WD Blue disk is working...
MSI RTX 3070 vedio card is working...
```