java5. Object中方法重写:构造计算机类,其中包含其配置信息:处理器、主板、内存、显示器、硬盘等设备,各个设备均有型号(字符串),特别的,处理器有主频(小数)和内核数(整数)、显示器有尺寸(整型)、内存和硬盘有容量数据(GB为单位)。请你尝试构造合适的类和类的关系来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。重写相关类的equals方法,使得两个配置完全相同的计算机为相同的计算机。重写相关类的toString函数,打印计算机的配置信息。
时间: 2023-12-03 13:47:36 浏览: 164
下面是java代码实现:
```java
public class Computer {
private CPU cpu;
private Motherboard motherboard;
private Memory memory;
private Display display;
private HardDisk hardDisk;
public Computer(CPU cpu, Motherboard motherboard, Memory memory, Display display, HardDisk hardDisk) {
this.cpu = cpu;
this.motherboard = motherboard;
this.memory = memory;
this.display = display;
this.hardDisk = hardDisk;
}
public double calculatePrice() {
return cpu.getPrice() + motherboard.getPrice() + memory.getPrice() + display.getPrice() + hardDisk.getPrice();
}
public void printConfig() {
System.out.println("CPU: " + cpu.getModel() + ", " + cpu.getFrequencyGHz() + "GHz, " + cpu.getNumOfCores() + " cores");
System.out.println("Motherboard: " + motherboard.getModel());
System.out.println("Memory: " + memory.getModel() + ", " + memory.getCapacityGB() + "GB");
System.out.println("Display: " + display.getModel() + ", " + display.getSizeInch() + " inch");
System.out.println("Hard Disk: " + hardDisk.getModel() + ", " + hardDisk.getCapacityGB() + "GB");
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Computer)) return false;
Computer computer = (Computer) o;
return Objects.equals(cpu, computer.cpu) &&
Objects.equals(motherboard, computer.motherboard) &&
Objects.equals(memory, computer.memory) &&
Objects.equals(display, computer.display) &&
Objects.equals(hardDisk, computer.hardDisk);
}
@Override
public int hashCode() {
return Objects.hash(cpu, motherboard, memory, display, hardDisk);
}
@Override
public String toString() {
return "Computer{" +
"cpu=" + cpu +
", motherboard=" + motherboard +
", memory=" + memory +
", display=" + display +
", hardDisk=" + hardDisk +
'}';
}
}
class CPU {
private String model;
private double frequencyGHz;
private int numOfCores;
private double price;
public CPU(String model, double frequencyGHz, int numOfCores, double price) {
this.model = model;
this.frequencyGHz = frequencyGHz;
this.numOfCores = numOfCores;
this.price = price;
}
public String getModel() {
return model;
}
public double getFrequencyGHz() {
return frequencyGHz;
}
public int getNumOfCores() {
return numOfCores;
}
public double getPrice() {
return price;
}
}
class Motherboard {
private String model;
private double price;
public Motherboard(String model, double price) {
this.model = model;
this.price = price;
}
public String getModel() {
return model;
}
public double getPrice() {
return price;
}
}
class Memory {
private String model;
private int capacityGB;
private double price;
public Memory(String model, int capacityGB, double price) {
this.model = model;
this.capacityGB = capacityGB;
this.price = price;
}
public String getModel() {
return model;
}
public int getCapacityGB() {
return capacityGB;
}
public double getPrice() {
return price;
}
}
class Display {
private String model;
private int sizeInch;
private double price;
public Display(String model, int sizeInch, double price) {
this.model = model;
this.sizeInch = sizeInch;
this.price = price;
}
public String getModel() {
return model;
}
public int getSizeInch() {
return sizeInch;
}
public double getPrice() {
return price;
}
}
class HardDisk {
private String model;
private int capacityGB;
private double price;
public HardDisk(String model, int capacityGB, double price) {
this.model = model;
this.capacityGB = capacityGB;
this.price = price;
}
public String getModel() {
return model;
}
public int getCapacityGB() {
return capacityGB;
}
public double getPrice() {
return price;
}
}
```
使用示例:
```java
public static void main(String[] args) {
CPU cpu = new CPU("Intel Core i7-9700K", 3.6, 8, 2500);
Motherboard motherboard = new Motherboard("ASUS ROG Maximus XI Hero", 2000);
Memory memory = new Memory("Corsair Vengeance RGB Pro", 32, 1400);
Display display = new Display("Dell Ultrasharp U2419HC", 24, 1500);
HardDisk hardDisk = new HardDisk("Samsung 970 EVO Plus", 500, 1000);
Computer computer1 = new Computer(cpu, motherboard, memory, display, hardDisk);
Computer computer2 = new Computer(cpu, motherboard, memory, display, hardDisk);
System.out.println(computer1.calculatePrice()); // 输出:7950.0
computer1.printConfig();
System.out.println(computer1.equals(computer2)); // 输出:true
System.out.println(computer1.toString());
}
```
阅读全文