[Java] 多态-01

动态绑定

   1, 要有继承   2, 要有重写  3, 父类引用指向子类对象

package com.bjsxt.chap03;

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 TestPolymoph_01 {

    public static void main(String[] args) {
        Cat c = new Cat("catName", "blue");
        Dog d = new Dog("dogName", "block");
        Lady l1 = new Lady("L1", c);
        Lady l2 = new Lady("L2", d);
        l1.myPetEnjoy();
        l2.myPetEnjoy();
    }
}



posted @ 2013-11-24 10:08  小尼人00  阅读(126)  评论(0编辑  收藏  举报