根据问题描述,我们可以设计如下的类结构:
- PC类:
- 属性:主板、内存、显卡、硬盘
- 方法:构造函数、打印信息方法
- 主板类:
- 属性:型号、芯片组、生产厂家
- 内存类:
- 属性:容量、型号、生产厂家
- 显卡类:
- 属性:型号、显存、生产厂家
- 硬盘类:
- 属性:容量、接口类型、生产厂家
下面是一个简单的实现:
```java
public class PC {
private Motherboard motherboard;
private Memory[] memories;
private GraphicsCard graphicsCard;
private HardDisk hardDisk;
public PC(Motherboard motherboard, Memory[] memories, GraphicsCard graphicsCard, HardDisk hardDisk) {
this.motherboard = motherboard;
this.memories = memories;
this.graphicsCard = graphicsCard;
this.hardDisk = hardDisk;
}
public void printInfo() {
System.out.println("PC信息:");
System.out.println("主板:" + motherboard.getModel() + "," + motherboard.getChipset() + "芯片组,生产厂家:" + motherboard.getManufacturer());
System.out.println("内存:");
for (Memory memory : memories) {
System.out.println(memory.getCapacity() + "GB " + memory.getModel() + ",生产厂家:" + memory.getManufacturer());
}
System.out.println("显卡:" + graphicsCard.getModel() + ",显存:" + graphicsCard.getMemorySize() + "GB,生产厂家:" + graphicsCard.getManufacturer());
System.out.println("硬盘:" + hardDisk.getCapacity() + "GB,接口类型:" + hardDisk.getInterfaceType() + ",生产厂家:" + hardDisk.getManufacturer());
}
}
public class Motherboard {
private String model;
private String chipset;
private String manufacturer;
public Motherboard(String model, String chipset, String manufacturer) {
this.model = model;
this.chipset = chipset;
this.manufacturer = manufacturer;
}
public String getModel() {
return model;
}
public String getChipset() {
return chipset;
}
public String getManufacturer() {
return manufacturer;
}
}
public class Memory {
private int capacity;
private String model;
private String manufacturer;
public Memory(int capacity, String model, String manufacturer) {
this.capacity = capacity;
this.model = model;
this.manufacturer = manufacturer;
}
public int getCapacity() {
return capacity;
}
public String getModel() {
return model;
}
public String getManufacturer() {
return manufacturer;
}
}
public class GraphicsCard {
private String model;
private int memorySize;
private String manufacturer;
public GraphicsCard(String model, int memorySize, String manufacturer) {
this.model = model;
this.memorySize = memorySize;
this.manufacturer = manufacturer;
}
public String getModel() {
return model;
}
public int getMemorySize() {
return memorySize;
}
public String getManufacturer() {
return manufacturer;
}
}
public class HardDisk {
private int capacity;
private String interfaceType;
private String manufacturer;
public HardDisk(int capacity, String interfaceType, String manufacturer) {
this.capacity = capacity;
this.interfaceType = interfaceType;
this.manufacturer = manufacturer;
}
public int getCapacity() {
return capacity;
}
public String getInterfaceType() {
return interfaceType;
}
public String getManufacturer() {
return manufacturer;
}
}
public class Main {
public static void main(String[] args) {
Motherboard motherboard1 = new Motherboard("ASUS Prime B450M-A", "AMD B450", "ASUS");
Memory[] memories1 = {new Memory(8, "Kingston HyperX Fury", "Kingston"), new Memory(8, "Kingston HyperX Fury", "Kingston")};
GraphicsCard graphicsCard1 = new GraphicsCard("Gigabyte GeForce GTX 1650", 4, "Gigabyte");
HardDisk hardDisk1 = new HardDisk(240, "SATA III", "Kingston");
PC pc1 = new PC(motherboard1, memories1, graphicsCard1, hardDisk1);
pc1.printInfo();
Motherboard motherboard2 = new Motherboard("ASUS ROG Maximus XII Hero", "Intel Z490", "ASUS");
Memory[] memories2 = {new Memory(16, "Corsair Vengeance RGB Pro", "Corsair"), new Memory(16, "Corsair Vengeance RGB Pro", "Corsair")};
GraphicsCard graphicsCard2 = new GraphicsCard("ASUS ROG Strix GeForce RTX 3080", 10, "ASUS");
HardDisk hardDisk2 = new HardDisk(2_000, "PCIe NVMe", "Samsung");
PC pc2 = new PC(motherboard2, memories2, graphicsCard2, hardDisk2);
pc2.printInfo();
}
}
```
在main方法中,我们分别创建了两个PC类对象,并打印了它们的信息。