码迷,mamicode.com
首页 > 其他好文 > 详细

0923-----In class to practive

时间:2016-09-23 13:09:49      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 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 }

 

0923-----In class to practive

标签:

原文地址:http://www.cnblogs.com/kaililikai/p/5899267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!