本帖最后由 HM刘志朋 于 2013-5-18 22:04 编辑
- class Animal
- {
- private String name;
- Animal(String name)
- {
- this.name = name;
- }
- public void enjoy()
- {
- System.out.println("叫声...");
- }
- }
- class Cat extends Animal
- {
- private String eyesColor;
- Cat(String n,String c)
- {
- super(n);//这里的super(n)是调用父类的构造方法的意思吗?可以调用一个吗?
- eyesColor = c;
- }
- public void enjoy()
- {
- System.out.println("猫叫声...");
- }
- }
- class Dog extends Animal
- {
- private String furColor;
- Dog(String n,String c)
- {
- super(n);
- furColor = c;
- System.out.println(n);
- }
- 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 Demo
- {
- public static void main(String[] args)
- {
- Cat c = new Cat("catname","bule");
- System.out.println(c);
- Dog d = new Dog("dogname","black");
- Lady l1 = new Lady("l1",c);
- Lady l2 = new Lady("l2",d);
- l1.myPetEnjoy();
- l2.myPetEnjoy();
- }
- }
复制代码 |