多态要满足三个条件:
1.要有继承 2.要有重写 3.父类引用指向子类对象- class Animal {
- private String name;
- Animal(String name) { this.name = name;}
- public void enjoy(){
- System.out.println("叫声......");
- }
- //public 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();}
- }