Define an interface CanFly, the method described flying public void fly ();

1, using knowledge-based interface complete with the following requirements:
(1) defines an interface CanFly, the method described fly Fly void public ();
(2) define class aircraft and birds, CanFly implement the interface.
(3) define a test class, test aircraft and birds. Define a test class makeFly () method, let things fly fly. 
(4) aircraft objects and then create a bird objects in the main method and call makeFly () method in the main method, let the plane take off and birds.

 

CanFly.java 

Package Penalty for com.fs.test; 

// represents the flying things 
public  interface CanFly {
     public  abstract  void Fly (); 
} 


Plane.java 

Package Penalty for com.fs.test; 

// airplanes
 // realized things fly ability of 
public  class plane the implements CanFly { 
    
    public  void fly () { 
        System.out.println ( "I am a plane, I can fly" ); 
    } 

} 

Bird.java 

Package Penalty for com.fs.test; 

// birds 
public  class Bird the implements{CanFly 
    
    public  void Fly () {
         // automatic generation method 
        System.out.println ( "I am a bird, I can fly" ); 
    } 

} 


Test.java 


Package com.fs.test; 

public  class the Test {
     public  static  void main (String [] args) {
         // instantiate aircraft 
        plane P = new new plane ();
         new new the Test () makeFly (P);. 
        
        // instantiate a bird 
        Bird B = new new Bird ();
         new new . the Test () makeFly (b); 
        
        
    } 
    
    // let things fly flying 
    public void makeFly (the p-CanFly) { 
        
    
    p.fly (); 
    } 


The result: I am a plane, I can fly my birds, I can fly

 

Guess you like

Origin www.cnblogs.com/ooo888ooo/p/11099952.html