laptop
Notebook (laptop) usually have to use a USB device functions. In the production, notebooks have been reserved can insert a USB device's USB port, but specifically what USB devices, notebook manufacturers do not care, as long as compliant with the USB specification device can be. Defined USB interface, with the most basic function is turned on and off function. To be able to use the mouse and keyboard on the computer, the mouse and keyboard must also comply with the USB specification, to achieve a USB interface, the mouse and keyboard otherwise produced can not be used.
case study
- Described notebook class that implements the notebook using a USB mouse, USB keyboard
- USB interface, includes opening function, closing function
- Notebook class that contains the function run, shutdown function, a USB device functions
- Mouse class method, to achieve a USB interface, and have clicked
- Keyboard class, to achieve a USB interface, with percussion method
Case realization
USB interface definitions
Package demo07; public interface the USB { public abstract void Open (); // Open the device public abstract void Close (); // Close device }
Mouse class definitions
Package demo07; // mouse USB device is a public class Mouse the implements USB { @Override public void Open () { System.out.println ( "Mouse open" ); } @Override public void Close () { System.out.println ( "off mouse" ); } public void the click () { System.out.println ( "mouse click" ); } }
Keyboard class definitions
Package demo07; // keyboard USB device is a public class Keyboard the implements USB { @Override public void Open () { System.out.println ( "keyboard open" ); } @Override public void Close () { System.out.println ( "Close keyboard" ); } public void type () { System.out.println ( "input" ); } }
Computer class definitions
Package demo07; public class Computer { public void the powerOn () { System.out.println ( "laptop boot" ); } public void a powerOff () { System.out.println ( "notebook is off" ); } // use the method of the USB device, the method using the interface as the parameter public void useDevice (USB USB) { usb.open (); // open device IF (USB the instanceof Mouse) { // sure Analyzing Mouse mouse = (Mouse) usb; // downcast mouse.click (); } The else IF (USB the instanceof Keyboard) { // first determine Keyboard Keyboard = (Keyboard) USB; // downward transition keyboard.type (); } usb.close (); // Close device } }
Defines the test class
Package Penalty for demo07; public class DemoMain { public static void main (String [] args) { // First create a laptop Computer Computer = new new Computer (); computer.powerOn (); // Prepare a mouse for computer use // mouse mouse = new new mouse (); // firstly upward transition USB usbmouse = new new mouse (); // multi-state writing // parameter is a USB type, I just passed into the USB mouse is computer.useDevice (usbmouse); / / create a USB keyboard keyboard keyboard = new new keyboard ();// do not use multi-state writing // method USB type parameter is passed into the class object is to achieve computer.useDevice (Keyboard); // correct wording! It has also undergone a transformation up // subclass objects, anonymous objects can also // computer.useDevice (new new Keyboard ()); // is the correct wording ; computer.powerOff () System.out.println ( === " =============== " ); Method ( 10.0); // correct wording, Double -> Double Method (20 is); // correct wording, int -> Double int A 30 = ; Method (A); // correct wording, int -> Double } public static void method(double num) { System.out.println(num); } }
Results of the