Java interfaces and polymorphism practice

 

 

 

 

 

 

We realize USB mouse and keyboard interface, mouse and keyboard we became a USB device, this time we can put it to use a laptop inside

 

 

. 1  Package com.biggw.day10.demo07;
 2  
. 3  / ** 
. 4  * @author GW
 . 5  * 0002 @date 2019/11/2 PM 16:46
 . 6   * / 
. 7  public  class Computer {
 . 8      public  void Open () {
 . 9          System.out.println ( "Turn on the computer!" );
 10      }
 . 11  
12 is      public  void Close () {
 13 is          System.out.println ( "off the computer!" );
 14      }
 15  
16      public  void useDevice (Usb USB) {
17         if (usb instanceof KeyBorad) {
18             KeyBorad keyBorad = (KeyBorad) usb;
19             keyBorad.open();
20             keyBorad.type();
21             keyBorad.close();
22         } else if (usb instanceof Mouse) {
23             Mouse mouse = (Mouse) usb;
24             mouse.open();
25             mouse.click();
26             mouse.close();
27         }
28     }
29 }
Computer
 1 package com.biggw.day10.demo07;
 2 
 3 /**
 4  * @author gw
 5  * @date 2019/11/2 0002 下午 16:42
 6  */
 7 public interface Usb {
 8     public abstract void open();
 9 
10     public abstract void close();
11 }
Usb Interface
. 1  Package com.biggw.day10.demo07;
 2  
. 3  / ** 
. 4  * @author GW
 . 5  * 0002 @date 2019/11/2 PM 16:44
 . 6   * / 
. 7  public  class Mouse the implements Usb {
 . 8      @Override
 . 9      public  void open () {
 10          System.out.println ( "open mouse!" );
 . 11      }
 12 is  
13 is      @Override
 14      public  void Close () {
 15          System.out.println ( "off mouse!" );
 16     }
 . 17  
18 is      public  void the Click () {
 . 19          System.out.println ( "mouse click!" );
 20      }
 21 }
Mouse Usb Interface implementation class
. 1  Package com.biggw.day10.demo07;
 2  
. 3  / ** 
. 4  * @author GW
 . 5  * 0002 @date 2019/11/2 PM 16:43
 . 6   * / 
. 7  public  class Keyborad the implements Usb {
 . 8      @Override
 . 9      public  void open () {
 10          System.out.println ( "open the keyboard!" );
 . 11      }
 12 is  
13 is      @Override
 14      public  void Close () {
 15          System.out.println ( "Close keyboard!" );
 16     }
 . 17  
18 is      public  void type () {
 . 19          System.out.println ( "keyboard to enter text!" );
 20      }
 21 }
KeyBoard Usb Interface implementation class
 1 package com.biggw.day10.demo07;
 2 
 3 /**
 4  * @author gw
 5  * @date 2019/11/2 0002 下午 16:52
 6  */
 7 public class Main {
 8     public static void main(String[] args) {
 9         Usb usb = new KeyBorad();
10         Usb usb1 = new Mouse();
11 
12         Computer computer = new Computer();
13         computer.open();
14         computer.useDevice (USB);
 15          computer.useDevice (USB1);
 16          computer.close ();
 . 17  
18 is          / * mouse click!
 19          to open the computer!
 20          open the keyboard!
 21          keyboard to enter text!
 22          to close the keyboard!
 23          open mouse!
24          Close mouse!
 25          off the computer! * / 
26      }
 27 }
Main method entry

 

Guess you like

Origin www.cnblogs.com/biggw/p/11782843.html