JAVA learning-Interface Usage method 2 (Inter-interface polymorphism), java Polymorphism
1 // There are also polymorphism 2 public class TestInterface {3 4 public static void main (String [] args) {5 Duck d = new Duck () between the interface and the specific implementation class (); 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 (); // virtual method call 13} 14 public static void test2 (timer mer s) {// timer mer s = new Duck (); 15 s. swim (); 16} 17 public static void test3 (Fli Er f) {// Flier f = new Duck (); 18 f. fly (); 19} 20} 21 22 interface Runner {23 public abstract void run (); 24} 25 interface timer mer {26 void swim (); 27} 28 interface Flier {29 void fly (); 30} 31 32 class Duck implements Runner, Timer mer, Flier {33 34 @ Override35 public void fly () {36 System. out. println ("The Ugly Duckling can also become a white swan! "); 37} 38 39 @ Override40 public void swim () {41 System. out. println (""); 42} 43 44 @ Override45 public void run () {46 System. out. println ("duck ass twisting"); 47} 48 49}