package Tian; public class Actor{ String name; Pet pet;
package Tian; public final class Bear extends Pet { public void run(){ System.out.println("我是雄二,我喜欢蜂蜜"); } }
package Tian; public class Cat extends Pet{ public void run(){ System.out.println("我是小猫,我正在给主人收集金币"); } }
public Actor(String name, Pet pet) { super(); this.name = name; this.pet=pet; } //主人正在跑的方法 public void run(){ System.out.println(name+"正在跑"); pet.run(); } }
package Tian; public class Dog extends Pet{ public void run(){ System.out.println("我是旺财,我为主人做贡献"); } }
package Tian; public class Pet { public void run(){ System.out.println("就是跑"); } }
package Tian; public class Test { /** * @param args */ public static void main(String[] args) { Actor a=new Actor("金枪小帅",new Bear()); a.run(); } }
时间: 2024-10-10 01:29:55