Using polymorphic, simple computerized device connected to the case usb

package cn.learn.Practice03;

public interface UsbInterface {
    void open();  //打开usb
    void close(); //关闭usb
}
Package cn.learn.Practice03; 

public  class Keyboard the implements UsbInterface { 
    @Override 
    public  void Open () { 
        System.out.println ( "keyboard open" ); 
    } 

    @Override 
    public  void Close () { 
        System.out.println ( " keyboard Close " ); 
    } 
    public  void type () { 
        System.out.println ( " keyboard to enter text " ); 
    } 
}
Package cn.learn.Practice03; 

public  class Mouse the implements UsbInterface { 
    @Override 
    public  void Open () { 
        System.out.println ( "open mouse connector" ); 
    } 

    @Override 
    public  void Close () { 
        System.out.println ( " Close mouse " ); 
    } 

    public  void the click () { 
        System.out.println ( " mouse click " ); 
    } 
}
Package cn.learn.Practice03; 

public  class LapTop { 

    public  void the powerOn () { 
        System.out.println ( "notebook boot" ); 
    } 
    public  void a powerOff () { 
        System.out.println ( "notebook off" ); 
    } 

    / / computer use usb, as the interface parameter list 
    public  void useDevice (UsbInterface USB) { 
        usb.open ();   // actual call is a mouse or a keyboard open method 

        // if the incoming is mouse, then the parameters are downcast , clicking 
        IF (USB the instanceof Mouse) {
             // parameters transformation 
            Mouse m =(Mouse) USB;
             // clicks 
            m.click (); 
        } 
        // if Keyboard is passed, then the downward transition parameter, clicking 
        IF (USB the instanceof Keyboard) {
             // Parameters transition 
            Keyboard K = (Keyboard ) USB;
             // for typing 
            k.type (); 
        } 

        usb.close (); // Close device 
    } 
}
. 1  Package cn.learn.Practice03;
 2  
. 3  public  class the Enter {
 . 4      public  static  void main (String [] args) {
 . 5          // First create a laptop 
. 6          LapTop One = new new LapTop ();
 . 7          one.powerOn ();
 . 8  
. 9  
10          // prepare a mouse 
. 11          mouse mouse = new new mouse ();
 12 is  
13 is          // mouse.open (); this method also has an interface, the same name, compile, running in the right priority subclass 
14          / * 
15  
16          ? ? correct wording, seemingly passed is an object, the actual implementation class is automatically upcast
 17         eg: int a = 10; method (a); the argument list is Double
 18          inappropriate Mouse can be seen as int
 19          can even write so one.useDevice (new mouse ()); - anonymous objects generate
 20          automatic transition to Mouse implementation class, is passed to useDevice (UsbInterface USB);
 21 is  
22 is           * / 
23 is          // call using a computer mouse 
24          one.useDevice (mouse); // trigger click method
 25  
26 is          // another way, prepare a keyboard, upcast 
27          UsbInterface Keyboard = new new Keyboard ();
 28          // keyboard.open (); this method also has an interface, the same name, compile, running in the right priority subclass
 29          // ((Keyboard) Keyboard) .Type (); not the method uses its own separate Comparative above
 30          //Calling computer using the keyboard 
31 is          one.useDevice (Keyboard);
 32  
33 is  
34 is          one.powerOff ();
 35      }
 36  
37 [ }

 

Guess you like

Origin www.cnblogs.com/huxiaobai/p/11470310.html