要求:
请实现笔记本电脑使用USB鼠标、USB键盘的例子
1. USB 接口:包含打开设备、关闭设备功能
2. 笔记本类:包含开机功能、关机功能、使用 USB 设备功能
3. 鼠标类:实现 USB 接口,并具备点击功能
4. 键盘类:实现 USB 接口,并具备输入功能
(不需要具体实现,给出框架即可)
代码:一个接口+四个类(键盘类+鼠标类+电脑类+测试类)
package csdn;import javax.swing.*;public class computer {public void powerOn(){System.out.println("打开笔记本电脑");}public void powerOff(){System.out.println("关闭笔记本电脑");}public void useDevice(IUSB iusb){iusb.openDevice(); //打开服务if(iusb instanceof Mouse){Mouse mouse=(Mouse)iusb; //向下转型mouse.click();}else if(iusb instanceof keyBoard){keyBoard keyBoard=(keyBoard)iusb; //向下转型keyBoard.input();}iusb.closeDevice(); //关闭服务}
}
package csdn;public interface IUSB {void openDevice(); //打开服务void closeDevice(); //关闭服务
}
package csdn;public class keyBoard implements IUSB {@Overridepublic void openDevice() {System.out.println("打开键盘");}@Overridepublic void closeDevice() {System.out.println("关闭键盘");}public void input(){System.out.println("敲击键盘输入");}
}
package csdn;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;public class Mouse implements IUSB {@Overridepublic void openDevice() {System.out.println("打开鼠标服务");}@Overridepublic void closeDevice() {System.out.println("关闭鼠标服务");}public void click(){System.out.println("点击鼠标");}
}
package csdn;public class test {public static void main(String[] args) {computer computer=new computer();Mouse mouse=new Mouse();keyBoard keyBoard=new keyBoard();computer.useDevice(mouse); //向上转型System.out.println("===========");computer.useDevice(keyBoard); //向上转型}
}