Analog computer USB interface
//Computer class:
/** computer class. The computer has a USB slot * / public class computer {private USB; / / indicates the USB slot on the computer. / / simulate connecting the USB device to the computer. Public void setusb (USB usbdevice) {USB = usbdevice;}// Simulate the operation of the USB device on the computer public void usbdevicework() {if (USB = = null) {system.out.println ("USB device has not been inserted yet"); return;} usb. work(); }
}
//USB class:
/** USB interface, USB is a protocol * / Public Interface USB {void work(); / / USB device works
}
//Keyboard class:
/** USB interface keyboard * / public class keyboard implements USB {@ override public void work() {system.out.println ("USB interface keyboard starts working");}
}
//Mouse class:
/** USB interface mouse * / public class mouse implements USB {@ override public void work() {system.out.println ("USB mouse starts working");}
}
Test demo class:
Public class demo {public static void main (string [] args) {/ / there is a computer first, computer Lenovo = new computer(); / / you need a keyboard, keyboat = new keyboard(); / / insert the keyboard into the computer, lenovo.setusb (keyboat); / / the keyboard can work. Lenovo.usbdevicework(); / / connect a mouse, mouse mouse = new mouse(); lenovo.setusb (mouse) ; lenovo. usbDeviceWork(); } }