Java实现USB接口程序设计
2021-03-07 20:30
标签:pen dell false main bar oar rri 实现 lan USB接口程序设计 USB.java Mouse.java Keyboard.java Microphone.java Computer.java Test.java Java实现USB接口程序设计 标签:pen dell false main bar oar rri 实现 lan 原文地址:https://www.cnblogs.com/MirandaYuen/p/14257146.htmlpublic abstract class USB {
boolean stat=false;
abstract void open();
abstract void close();
}
public class Mouse extends USB{
@Override
public void open() {
stat=true;
System.out.println("鼠标启动了");
}
@Override
public void close() {
stat=false;
System.out.println("鼠标关闭了");
}
}
public class Keyboard extends USB{
@Override
public void open() {
stat=true;
System.out.println("键盘启动了");
}
@Override
public void close() {
stat=true;
System.out.println("键盘关闭了");
}
}
public class Microphone extends USB{
@Override
public void open() {
stat=true;
System.out.println("麦克风启动了");
}
@Override
public void close() {
stat=true;
System.out.println("麦克风关闭了");
}
}
public class Computer {
public USB[] getUsbArr() {
return UsbArr;
}
public void setUsbArr(USB[] usbArr) {
UsbArr = usbArr;
}
private USB[] UsbArr=new USB[3];
//遍历所有接口,如果有接口未打开,则把接口插上
public void connect()
{
for (USB usb : UsbArr) {
if (!usb.stat) {
usb.open();
}
}
}
//遍历所有接口,如果有接口未打开,则把接口插上
public void disconnect()
{
for (USB usb : UsbArr) {
if (usb.stat) {
usb.close();
}
}
}
//开电脑
public void openComputer()
{
connect();
System.out.println("电脑已打开");
}
public void closeComputer()
{
disconnect();
System.out.println("电脑已关闭");
}
}
public class Test {
public static void main(String[] args) {
Computer dell=new Computer();
dell.setUsbArr(new USB[]{new Mouse(),new Keyboard(),new Microphone()});
dell.openComputer();
dell.closeComputer();
}
}
上一篇:SpringMVC配置