JAVA学習--インタフェースの使い方2(インタフェース間の多態性)
5775 ワード
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 }