JAVA学习--接口使用方法2(接口间的多态性)

 1 //接口与具体的实现类之间也存在多态性
 2 public class TestInterface {
 3     
 4     public static void main(String[] args) {
 5         Duck d = new Duck();
 6         TestInterface.test1(d);
 7         TestInterface.test2(d);
 8         TestInterface.test3(d);
 9     }
10     
11     public static void test1(Runner r){//Runner r = new Duck();
12         r.run();//虚拟方法调用
13     }
14     public static void test2(Swimmer s){//Swimmer s = new Duck();
15         s.swim();
16     }
17     public static void test3(Flier f){//Flier f = new Duck();
18         f.fly();
19     }
20 }
21 
22 interface Runner{
23     public abstract void run();
24 }
25 interface Swimmer{
26     void swim();
27 }
28 interface Flier{
29     void fly();
30 }
31 
32 class Duck implements Runner,Swimmer,Flier{
33 
34     @Override
35     public void fly() {
36         System.out.println("丑小鸭也可以变成白天鹅!");
37     }
38 
39     @Override
40     public void swim() {
41         System.out.println("红掌拨清波");
42     }
43 
44     @Override
45     public void run() {
46         System.out.println("鸭子屁股扭扭的走路");
47     }
48     
49 } 

 

posted @ 2015-01-30 22:12  浮云骑士  阅读(400)  评论(0编辑  收藏  举报