点击运行
import java.io.IOException; interface Animal { public void display(); } class Dog { Animal s; public Dog(Animal s){ this.s = s; } public void display(){ System.out.println("Dog"); s.display(); } } class Cat implements Animal { public Cat(){} public void display(){ System.out.println("Cat"); } } class Cow implements Animal { public Cow(){} public void display(){ System.out.println("Cow"); } } public class Test { public static void main(String args[]) throws IOException { Animal b = new Cat(); Animal c = new Cow(); Dog a = new Dog(b); //a.display() 将会打印 dog 和 cat a.display(); Dog a1 = new Dog(c); //a.display() 将会打印 dog 和 Cow a1.display(); } }
运行结果 :
正在执行...