多态也叫动态绑定或者后期绑定,运行时绑定,含义是在运行时根据对象的类型进行绑定。多态使我们只需要考虑编写与基类打交道的代码,发送消息给某个基类,具体调用时再通过导出类的方法进行调用,这叫做向上转型。
public class Shape {
public void draw() {}
public void erase() {}
} ///:~基类
public class Square extends Shape {
public void draw() { print("Square.draw()"); }
public void erase() { print("Square.erase()"); }
} ///:~
public class Triangle extends Shape {
public void draw() { print("Triangle.draw()"); }
public void erase() { print("Triangle.erase()"); }
} ///:~
public class Circle extends Shape {
public void draw() { print("Circle.draw()"); }
public void erase() { print("Circle.erase()"); }
} ///:~导出类
public class RandomShapeGenerator {
private Random rand = new Random(47);
public Shape next() {
switch(rand.nextInt(3)) {
default:
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
}//调用next()方法得到的是shape的引用而不是具体类型
}
} ///:~工厂
public class Shapes {
private static RandomShapeGenerator gen =
new RandomShapeGenerator();
public static void main(String[] args) {
Shape[] s = new Shape[9];
// Fill up the array with shapes:
for(int i = 0; i < s.length; i++)
s[i] = gen.next();
// Make polymorphic method calls:
for(Shape shp : s)
shp.draw();//当调用draw方法时,产生与类型有关的特定信息,有关方法使用动态绑定
}
} /* Output:
Triangle.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Circle.draw()
*///:~
域不是多态的,通常将域设置为private,一般不会将域和导出类中的域赋予相同的名字,避免混淆。静态的方法同样不是多态的,它只和类相关联而不是和对象相关联。
构造器内部出现动态绑定的方法:
class Glyph {
void draw() { print("Glyph.draw()"); }
Glyph() {
print("Glyph() before draw()");
draw();
print("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
print("RoundGlyph.RoundGlyph(), radius = " + radius);
}
void draw() {
print("RoundGlyph.draw(), radius = " + radius);
}
}
public class PolyConstructors {
public static void main(String[] args) {
new RoundGlyph(5);
}
} /* Output:
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
*///:~
初始化的顺序:
1. 将分配给对象的存储空间初始化成二进制的0,或者把引用初始化为null
2. 调用基类构造器,调用被覆盖后的draw()方法,此时radius就是0
3. 调用成员的初始化方法
4. 调用导出类的构造器
协变返回类型
class Grain {
public String toString() { return "Grain"; }
}
class Wheat extends Grain {
public String toString() { return "Wheat"; }
}
class Mill {
Grain process() { return new Grain(); }
}
class WheatMill extends Mill {
Wheat process() { return new Wheat(); }
}
public class CovariantReturn {
public static void main(String[] args) {
Mill m = new Mill();
Grain g = m.process();
System.out.println(g);
m = new WheatMill();
g = m.process();
System.out.println(g);
}
} /* Output:
Grain
Wheat
*///:~
导出类覆盖基类的方法,返回值可以是基类方法返回值的导出类型。