:构造计算机类,其中包含其配置信息:处理器、主板、内存、显示器、硬盘等设备,各个设备均有型号(字符串),特别的,处理器有主频(小数)和内核数(整数)、显示器有尺寸(整型)、内存和硬盘有容量数据(GB为单位)。请你尝试构造合适的类和类的关系来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。重写相关类的equals方法,使得两个配置完全相同的计算机为相同的计算机。重写相关类的toString函数,打印计算机的配置信息。用java
时间: 2024-03-03 07:53:38 浏览: 95
实现。
可以定义一个Computer类,其中包含Processor(处理器)、Motherboard(主板)、Memory(内存)、Display(显示器)、HardDisk(硬盘)这五个设备类。具体代码如下:
```java
public class Computer {
private Processor processor;
private Motherboard motherboard;
private Memory memory;
private Display display;
private HardDisk hardDisk;
public Computer(Processor processor, Motherboard motherboard, Memory memory, Display display, HardDisk hardDisk) {
this.processor = processor;
this.motherboard = motherboard;
this.memory = memory;
this.display = display;
this.hardDisk = hardDisk;
}
public double getPrice() {
return processor.getPrice() + motherboard.getPrice() + memory.getPrice() + display.getPrice() + hardDisk.getPrice();
}
public String toString() {
return "Computer configuration: \n" + processor.toString() + motherboard.toString() + memory.toString() + display.toString() + hardDisk.toString();
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Computer)) {
return false;
}
Computer other = (Computer) obj;
return this.processor.equals(other.processor) &&
this.motherboard.equals(other.motherboard) &&
this.memory.equals(other.memory) &&
this.display.equals(other.display) &&
this.hardDisk.equals(other.hardDisk);
}
}
class Processor {
private String model;
private double frequency;
private int cores;
private double price;
public Processor(String model, double frequency, int cores, double price) {
this.model = model;
this.frequency = frequency;
this.cores = cores;
this.price = price;
}
public double getPrice() {
return price;
}
public String toString() {
return "Processor: " + model + ", frequency: " + frequency + "GHz, cores: " + cores + "\n";
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Processor)) {
return false;
}
Processor other = (Processor) obj;
return this.model.equals(other.model) &&
this.frequency == other.frequency &&
this.cores == other.cores &&
this.price == other.price;
}
}
class Motherboard {
private String model;
private double price;
public Motherboard(String model, double price) {
this.model = model;
this.price = price;
}
public double getPrice() {
return price;
}
public String toString() {
return "Motherboard: " + model + "\n";
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Motherboard)) {
return false;
}
Motherboard other = (Motherboard) obj;
return this.model.equals(other.model) &&
this.price == other.price;
}
}
class Memory {
private String model;
private int capacity;
private double price;
public Memory(String model, int capacity, double price) {
this.model = model;
this.capacity = capacity;
this.price = price;
}
public double getPrice() {
return price;
}
public String toString() {
return "Memory: " + model + ", capacity: " + capacity + "GB\n";
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Memory)) {
return false;
}
Memory other = (Memory) obj;
return this.model.equals(other.model) &&
this.capacity == other.capacity &&
this.price == other.price;
}
}
class Display {
private String model;
private int size;
private double price;
public Display(String model, int size, double price) {
this.model = model;
this.size = size;
this.price = price;
}
public double getPrice() {
return price;
}
public String toString() {
return "Display: " + model + ", size: " + size + " inch\n";
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Display)) {
return false;
}
Display other = (Display) obj;
return this.model.equals(other.model) &&
this.size == other.size &&
this.price == other.price;
}
}
class HardDisk {
private String model;
private int capacity;
private double price;
public HardDisk(String model, int capacity, double price) {
this.model = model;
this.capacity = capacity;
this.price = price;
}
public double getPrice() {
return price;
}
public String toString() {
return "HardDisk: " + model + ", capacity: " + capacity + "GB\n";
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof HardDisk)) {
return false;
}
HardDisk other = (HardDisk) obj;
return this.model.equals(other.model) &&
this.capacity == other.capacity &&
this.price == other.price;
}
}
```
这样,我们就可以通过创建各个设备的对象,然后组合成一个计算机对象,并且可以计算计算机的价格、打印计算机的配置信息,并且可以判断两个计算机的配置是否完全相同。