两个重要概念: 1,向上转型 (子类到父类) 2,向下转型(父类到子类)
java允许以上两种类型,且两种类型之间必须有继承关系。
什么是多态: 多种形态(编译和运行是两种形态),多种状态。
父类型引用指向子类型对象(编译阶段静态绑定父类型方法,运行阶段动态绑定子类型对象的方法)
java程序分为编译阶段和巡行阶段。
编译阶段:编译器在检查语法的时候,会静态绑定(编译阶段不会new对象) 即找到父类中和子类同名的方法,并进行静态绑定。编译通过。
运行阶段:在堆内存中实际创建的是子类的对象,所以在调用方法的时候,实际调用的是子类的方法,这个过程属于运行阶段绑定。(运行阶段绑定属于动态绑定)
****什么时候向下转型,当父类型的引用想访问子类型中特有的方法的时候,此时就必须进行向下转型。
class Animal{ public void move(){ System.out.println("animal is moving"); } } class Cat extends Animal{ public void move(){ System.out.println("cat is catching the mouth"); //此处为方法覆盖 } public void walk(){ System.out.println("cat is walking"); } } class Bird extends Animal{ public void move(){ System.out.println("bird is flying"); //此处也是方法覆盖override 覆盖的是Animal中的move方法 } } public class DtTest{ public static void main(String[] args){ Animal cat = new Cat(); //此处就是动态绑定,即在编译阶段,声明一盒animal类型的变量cat,并不生成对象,因为在Animal中有move对象,所以可以编译通过 cat.move(); Animal bird = new Bird(); //在运行阶段,会在堆内存中生成cat对象,且在实际运行的时候 实际是调用cat类的(子类的)move方法。 bird.move(); Animal cat1 = new Cat(); //cat1.walk(); //错误: 找不到符号 Cat x = (Cat)cat1; //此处就是强制类型转换,专业术语为向下转型,一般情况下,强制类型转化多称呼与基本数据类型的时候。 x.walk(); } }