多种形态
变量的引用形式:
1.本态引用:左边的变量与右边的对象是同一种类型
Person p = new Person();
2.多态引用:左边的变量是父类类型,右边的对象是子类对象
Person p = new Woman();
多态表现出来的特征:编译类型与运行时类型不一致
1.编译的时候,按照父类的类型进行编译的
2.执行的方法,按照子类进行运行,是执行子类重写的方法
## 编译看左,运行看右
## 前提:1.继承;2.重写;3.多态引用
## 注意:多态和属性无关,只说方法
多态的好处:
使得程序员编写代码更加灵活
多态的应用:
1.多态数组
数组的元素是父类的类型,实际存储的是子类对象
用这样的数组,就可以统一管理所有子类的对象
2.多态参数
形参是父类的类型,实参是子类的对象
When nothing seems to help, I go look at a stonecutter hammering away at his rock, perhaps a hundred times without as much as a crack showing in it. Yet at the hundred and first blow it will split in two, and I know it was not that blow that did it, but all that had gone before. -- Jacob Riis