用java代码模拟PC机上能插装并运行各种PCI设备 每台PC机上都有一个PIC插槽,插槽可插入的设备包括声卡、网卡、显卡等等。任何一个PCI设备都能启动、运行、停止。请模拟PC机上能插装并运行各种PCI设备。要求设计一个PIC接口;DisplayCard类表示显卡并实现PIC接口;NetCard类表示网卡并实现PIC接口;SoundCard类表示声卡并实现PIC接口;MainBoard类表示计算机主板拥有private权限的PIC[]属性的成员变量pics;Computer类用于表示计算机,拥有private权限的MainBoard属性的mainboard成员变量。
时间: 2023-12-14 21:40:00 浏览: 122
以下是示例代码:
```
interface PIC {
void start();
void run();
void stop();
}
class DisplayCard implements PIC {
@Override
public void start() {
System.out.println("Display card starts.");
}
@Override
public void run() {
System.out.println("Display card runs.");
}
@Override
public void stop() {
System.out.println("Display card stops.");
}
}
class NetCard implements PIC {
@Override
public void start() {
System.out.println("Net card starts.");
}
@Override
public void run() {
System.out.println("Net card runs.");
}
@Override
public void stop() {
System.out.println("Net card stops.");
}
}
class SoundCard implements PIC {
@Override
public void start() {
System.out.println("Sound card starts.");
}
@Override
public void run() {
System.out.println("Sound card runs.");
}
@Override
public void stop() {
System.out.println("Sound card stops.");
}
}
class MainBoard {
private PIC[] pics;
public MainBoard(PIC[] pics) {
this.pics = pics;
}
public void startAllPICs() {
for (PIC pic : pics) {
pic.start();
}
}
public void runAllPICs() {
for (PIC pic : pics) {
pic.run();
}
}
public void stopAllPICs() {
for (PIC pic : pics) {
pic.stop();
}
}
}
class Computer {
private MainBoard mainboard;
public Computer(MainBoard mainboard) {
this.mainboard = mainboard;
}
public void start() {
mainboard.startAllPICs();
System.out.println("Computer starts.");
}
public void run() {
mainboard.runAllPICs();
System.out.println("Computer runs.");
}
public void stop() {
mainboard.stopAllPICs();
System.out.println("Computer stops.");
}
}
```
使用示例:
```
public static void main(String[] args) {
DisplayCard displayCard = new DisplayCard();
NetCard netCard = new NetCard();
SoundCard soundCard = new SoundCard();
PIC[] pics = {displayCard, netCard, soundCard};
MainBoard mainboard = new MainBoard(pics);
Computer computer = new Computer(mainboard);
computer.start();
computer.run();
computer.stop();
}
```
输出结果:
```
Display card starts.
Net card starts.
Sound card starts.
Computer starts.
Display card runs.
Net card runs.
Sound card runs.
Computer runs.
Display card stops.
Net card stops.
Sound card stops.
Computer stops.
```
阅读全文