天天看點

JAVA new 不new_java中沒new對象使用.和new了對象使用???

abstractclassAnimal{ privateStringname; Animal(Stringname){this.name=name;}   public abstract void enjoy();}abstract class Cat extends Animal {  private String eyesColor;  Cat(String n,String c) {super(n); eyesColor = c;}    //public abstract void enjoy();}class Dog extends Animal {  private String furColor;  Dog(String n,String c) {super(n); furColor = c;}   public void enjoy() {    System.out.println("狗叫聲......");  }}class Bird extends Animal {  Bird() {    super("bird");  }  public void enjoy() {    System.out.println("鳥叫聲......");  }}class Lady {    private String name;    private Animal pet;    Lady(String name,Animal pet) {        this.name = name; this.pet = pet;///--問題這裡    }    public void myPetEnjoy(){pet.enjoy();}}public class Test {    public static void main(String args[]){        Cat c = new Cat("catname","blue");        Dog d = new Dog("dogname","black");        Bird b = new Bird();        //Lady l1 = new Lady("l1",c);        Lady l2 = new Lady("l2",d);        Lady l3 = new Lady("l3",b);       //l1.myPetEnjoy();        l2.myPetEnjoy();        l3.myPetEnjoy();    }}不是說用對象一定要new嗎.為什麼能向Animal指派.和使用Animal 裡面的方法enjoy呢代碼裡沒有new Animal();為什麼能使用和指派呢

展開