0923-----In class to practive---about polymorphism and the primary of instanceof

 1 package com.kai.li;
 2 
 3 /**
 4   *
 5   */
 6 public class FlyText{
 7     public static void main(String[] args){
 8         Plane p = new Plane();
 9         Bird b = new Bird();
10         makeFly(p);
11         makeFly(b);
12         makeFly("dddkkdkdk");
13         makeFly(20.5);
14         makeFly(new Object());
15     }
16     public static void makeFly(Object o){
17         if(!(o instanceof ICanFly))
18             System.out.println("I can't fly.");
19         else 
20             ((ICanFly)o).fly();
21     }
22 }
1 package com.kai.li;
2 /**
3   * 
4   */
5 public interface ICanFly{
6     void fly();
7 }
 1 package com.kai.li;
 2 
 3 /**
 4   *
 5   */
 6 public class Plane implements ICanFly{
 7     public void fly(){
 8         System.out.println("I am a plane,I can fly.");
 9     }
10 }
 1 package com.kai.li;
 2 
 3 /**
 4   *
 5   */
 6 public class Bird implements ICanFly{
 7     @Override
 8     public void fly(){
 9         System.out.println("I am a bird,I can fly.");
10     }
11 }

 

posted @ 2016-09-23 11:12  火山林风  阅读(170)  评论(0编辑  收藏  举报