1. 多态的含义
2. 方法的调用
package net.mindview.polymorphism;
//乐器
class Instrument {
public void play(Note i){
System.out.println("Instrument.play() ");
}
}
//风管乐器
class Wind extends Instrument {
@Override
public void play(Note i) {
System.out.println("Wind.play() ");
}
}
public class Music {
//曲调
public static void tune(Instrument i){
i.play(Note.MIDDLE_C);
}
public static void main(String[] args) {
Wind wind = new Wind();
tune(wind);
}
}
分析:这里有一个行为很特殊, 就是tune方法,他传递的参数Instrument,调用的也是Instrument的play方法,那么当我在Music中传递一个wind给tune时,他会知道我要调用的方法应该是Wind总的play方法,而不是Instrument中的方法么?是的, 可以.这个过程在前期绑定的时候肯定是不知道的, 他是在后期绑定的时候知道的.
下面来说说什么是绑定?
3. 什么样的程序是可扩展的?
4. 静态方法不具有多态性. 调用的时那个对象的方法,执行的就是那个对象的方法, 不会向上转型
5.构造器调用的顺序
6.忠告: 编写构造器的时候, 用尽可能简单的方法是对象进入正常状态,尽量避免调用其他方法
7.协变返回类型
package net.mindview.polymorphism;
class Grain {
@Override
public String toString() {
return "Grain";
}
}
class Wheat extends Grain {
@Override
public String toString() {
return "Wheat";
}
}
class Mill {
Grain process(){
return new Grain();
}
}
class WheatMill extends Mill {
Wheat process(){
return new Wheat();
}
}
public class GovariantReturn {
public static void main(String[] args) {
Mill m = new Mill();
Grain g = m.process();
System.out.println(g);
WheatMill w = new WheatMill();
Wheat wh = w.process();
System.out.println(wh);
}
}
8.纯继承