java中没new对象使用.和new了对象使用???
abstractclassAnimal{ privateStringname; Animal(Stringname){this.name=name;}...
abstract class Animal { private String name; Animal(String name) {this.name = name;} /* public void enjoy(){ System.out.println("叫声......"); } */ public abstract void enjoy();}abstract class Cat extends Animal { private String eyesColor; Cat(String n,String c) {super(n); eyesColor = c;} /* public void enjoy() { System.out.println("猫叫声......"); } */ //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();为什么能使用和赋值呢
展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询