Java代码,由Component派生出CPU类(CPU)和显卡类(Vedio)。CPU类增加了新的属性:核心数,Vedio类增加了属性:显存。
时间: 2023-07-16 19:14:23 浏览: 52
以下是Java代码:
```
import java.awt.Component;
public class CPU extends Component {
private int coreCount;
public CPU(int coreCount) {
this.coreCount = coreCount;
}
public int getCoreCount() {
return coreCount;
}
public void setCoreCount(int coreCount) {
this.coreCount = coreCount;
}
}
public class Vedio extends Component {
private int memorySize;
public Vedio(int memorySize) {
this.memorySize = memorySize;
}
public int getMemorySize() {
return memorySize;
}
public void setMemorySize(int memorySize) {
this.memorySize = memorySize;
}
}
```
相关问题
使用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在工作,**品牌的硬盘在工作,**品牌的显卡在工作)。
抽象类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...
```
1) 定义一个表示电脑组件的抽象类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在工作,**品牌的硬盘在工作,**品牌的显卡在工作)。
下面是该题的Python代码实现:
```python
# 定义抽象类 Component
class Component:
def __init__(self, name, price):
self.name = name
self.price = price
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
def get_price(self):
return self.price
def set_price(self, price):
self.price = price
def describe(self):
print("组件名称:{},价格:{}".format(self.name, self.price))
# 由 Component 派生出 CPU、Disk 和 Vedio 类
class CPU(Component):
def __init__(self, name, price, cores):
super().__init__(name, price)
self.cores = cores
def get_cores(self):
return self.cores
def set_cores(self, cores):
self.cores = cores
def describe(self):
print("CPU 名称:{},价格:{},核心数:{}".format(self.name, self.price, self.cores))
class Disk(Component):
def __init__(self, name, price, capacity):
super().__init__(name, price)
self.capacity = capacity
def get_capacity(self):
return self.capacity
def set_capacity(self, capacity):
self.capacity = capacity
def describe(self):
print("硬盘名称:{},价格:{},容量:{}".format(self.name, self.price, self.capacity))
class Vedio(Component):
def __init__(self, name, price, memory):
super().__init__(name, price)
self.memory = memory
def get_memory(self):
return self.memory
def set_memory(self, memory):
self.memory = memory
def describe(self):
print("显卡名称:{},价格:{},显存:{}".format(self.name, self.price, self.memory))
# 由 Component 派生出 Computer 类
class Computer(Component):
def __init__(self, cpu, disk, vedio):
self.cpu = cpu
self.disk = disk
self.vedio = vedio
def get_cpu(self):
return self.cpu
def set_cpu(self, cpu):
self.cpu = cpu
def get_disk(self):
return self.disk
def set_disk(self, disk):
self.disk = disk
def get_vedio(self):
return self.vedio
def set_vedio(self, vedio):
self.vedio = vedio
def describe(self):
print("电脑组件:")
self.cpu.describe()
self.disk.describe()
self.vedio.describe()
def work(self):
print("{} 品牌的 CPU 在工作,{} 品牌的硬盘在工作,{} 品牌的显卡在工作".format(self.cpu.get_name(), self.disk.get_name(), self.vedio.get_name()))
# 由 CPU、Disk 和 Vedio 分别派生出具有不同品牌的子类
class IntelCPU(CPU):
def __init__(self, cores):
super().__init__("Intel", 500, cores)
class AMDCPU(CPU):
def __init__(self, cores):
super().__init__("AMD", 400, cores)
class SeagateDisk(Disk):
def __init__(self, capacity):
super().__init__("Seagate", 300, capacity)
class ASUSVedio(Vedio):
def __init__(self, memory):
super().__init__("ASUS", 800, memory)
# 测试类
if __name__ == '__main__':
# 组装两台不同品牌的电脑
computer1 = Computer(IntelCPU(8), SeagateDisk(1000), ASUSVedio(4))
computer2 = Computer(AMDCPU(6), SeagateDisk(500), ASUSVedio(8))
# 输出电脑的组件信息
print("第一台电脑:")
computer1.describe()
print("第二台电脑:")
computer2.describe()
# 输出电脑的工作方式
print("第一台电脑工作方式:")
computer1.work()
print("第二台电脑工作方式:")
computer2.work()
```
输出结果为:
```
第一台电脑:
电脑组件:
CPU 名称:Intel,价格:500,核心数:8
硬盘名称:Seagate,价格:300,容量:1000
显卡名称:ASUS,价格:800,显存:4
第二台电脑:
电脑组件:
CPU 名称:AMD,价格:400,核心数:6
硬盘名称:Seagate,价格:300,容量:500
显卡名称:ASUS,价格:800,显存:8
第一台电脑工作方式:
Intel 品牌的 CPU 在工作,Seagate 品牌的硬盘在工作,ASUS 品牌的显卡在工作
第二台电脑工作方式:
AMD 品牌的 CPU 在工作,Seagate 品牌的硬盘在工作,ASUS 品牌的显卡在工作
```