Java接口组装一台计算机编写各组件厂商实现CPU,EMS,HardDisk接口
时间: 2024-10-24 19:13:49 浏览: 7
基于Java实现用类描述计算机中CPU的速度和硬盘的容量【100012713】
在Java中,我们可以继续扩展上述的计算机组件接口,为CPU、EMS(Enhanced Memory System)和硬盘等部件创建对应的接口,并由不同的厂商实现它们。这里给出一个简化的示例:
首先,定义一些基本的计算机组件接口:
```java
// 基础计算机组件接口
interface ComputerComponent {
void start();
void stop();
}
// CPU接口
interface CpuComponent extends ComputerComponent {
String getBrand();
int getCpuSpeed();
}
// EMS接口
interface EmscComponent extends ComputerComponent {
int getMemorySize();
}
// 硬盘接口
interface HarddiskComponent extends ComputerComponent {
String getModel();
long getTotalSpace();
}
```
然后,让不同厂商实现这些接口:
```java
// Intel CPU实现
class IntelCpu implements CpuComponent {
private String brand;
private int speed;
// ...(构造函数,getter和setter)
@Override
public String getBrand() {
return brand;
}
@Override
public int getCpuSpeed() {
return speed;
}
@Override
public void start() {
System.out.println("Intel CPU started");
}
@Override
public void stop() {
System.out.println("Intel CPU stopped");
}
}
// Kingston EMS实现
class KingstonEmc implements EmscComponent {
private int memorySize;
// ...(构造函数,getter和setter)
@Override
public int getMemorySize() {
return memorySize;
}
@Override
public void start() {
System.out.println("Kingston EMS started");
}
@Override
public void stop() {
System.out.println("Kingston EMS stopped");
}
}
// Seagate Hard Disk实现
class SeagateHarddisk implements HarddiskComponent {
private String model;
private long totalSpace;
// ...(构造函数,getter和setter)
@Override
public String getModel() {
return model;
}
@Override
public long getTotalSpace() {
return totalSpace;
}
@Override
public void start() {
System.out.println("Seagate hard disk started");
}
@Override
public void stop() {
System.out.println("Seagate hard disk stopped");
}
}
```
在主程序中,你可以选择特定厂商的产品实例化并操作:
```java
public class Main {
public static void main(String[] args) {
IntelCpu intelCpu = new IntelCpu();
KingstonEmc kingstonEmc = new KingstonEmc();
SeagateHarddisk seagateHarddisk = new SeagateHarddisk();
// 启动和停止各个组件
intelCpu.start();
kingstonEmc.start();
seagateHarddisk.start();
// 获取和显示详细信息
System.out.println("CPU: " + intelCpu.getBrand() + ", Speed: " + intelCpu.getCpuSpeed());
System.out.println("EMS: Size: " + kingstonEmc.getMemorySize());
System.out.println("Hard Disk: Model: " + seagateHarddisk.getModel() + ", Total Space: " + seagateHarddisk.getTotalSpace());
// 停止所有组件
intelCpu.stop();
kingstonEmc.stop();
seagateHarddisk.stop();
}
}
```
阅读全文