第8章 多态

在面向对象的程序设计语言中,多态是继数据抽象和继承之后的第三种基本特征.

1. 再论向上转型

对象既可以作为它自己本身的类型使用,也可以作为它的基类使用.而这种把对某个对象的引用视为对其基类的引用的做法被称为”向上转型” – 因为在继承书的画法中,基类是放置在上方的.

首先,既然几个例子都要演奏乐符 (Note) ,我们就应该在包中单独创建一个Note类.

1
2
3
4
5
6
7
//: polymorphism/music/Note.java
// Notes to play on musical instruments.
package polymorphism.music;
public enum Note {
MIDDLE_C, C_SHARP, B_FLAT; // Etc.
} ///:~

在这里,Wind是一种Instrument,因此可以从Instrument类继承.

1
2
3
4
5
6
7
8
9
10
//: polymorphism/music/Instrument.java
package polymorphism.music;
import static net.mindview.util.Print.*;
class Instrument {
public void play(Note n) {
print("Instrument.play()");
}
}
///:~
1
2
3
4
5
6
7
8
9
10
11
//: polymorphism/music/Wind.java
package polymorphism.music;
// Wind objects are instruments
// because they have the same interface:
public class Wind extends Instrument {
// Redefine interface method:
public void play(Note n) {
System.out.println("Wind.play() " + n);
}
} ///:~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//: polymorphism/music/Music.java
// Inheritance & upcasting.
package polymorphism.music;
public class Music {
public static void tune(Instrument i) {
// ...
i.play(Note.MIDDLE_C);
}
public static void main(String[] args) {
Wind flute = new Wind();
tune(flute); // Upcasting
}
} /* Output:
Wind.play() MIDDLE_C
*///:~

Music.tune()方法接受一个Instrument引用,同时也接受任何导出自Instrument 的类.在main()方法中,当一个Wind引用传递到tune()方法时,就会出现这种情况,而不需要任何类型转换.因为Wind到Instrument继承而来,所以Instrument的接口必定存在于Wind中.从Wind向上转型到Instrument可能会缩小接口,但不会比Instrument的全部接口更窄.

1.1 忘记对象类型

如果我们不管导出类的存在,编写的代码只是与基类打交道,会不会更好呢?

2. 转机

2.1 方法调用绑定

将一个方法调用同一个方法主体关联起来被称作绑定.若在程序执行前进行绑定(如果有的话,由编译器和连接程序实现),叫做前期绑定.

后期绑定的含义就是在运行时根据对象的类型进行绑定.后期绑定也叫做动态绑定或运行时绑定.如果一种语言想实现后期绑定,就必须具有某种机制,以便在运行时能判断对象的类型,从而调用恰当的方法.也就说说,编译器一直不知道对象的类型,但是方法调用机制能找到正确的方法体,并加以调用.

Java中除了static方法和final方法(private方法属于final方法)之外,其他所有的方法都是后期绑定.

对某个方法声明final,它可以防止其他人覆盖该方法.但更重要的一点是告诉编译器不需要对其进行动态绑定.这样,编译器就可以为final方法调用生成更有效的代码.

2.2 产生正确的行为

发送消息给某个对象时,让该对象去判定应该做什么事.

几何形状
向上转型可以像下面这条语句那么简单:

Shape s = new Circle();

这里,创建了一个Circle对象,并把得到的引用立即赋值给Shape,因为通过继承.Circle就是一种Shape.

假设你调用一个基类方法(它已在导出类中被覆盖)

s.draw();

由于后期绑定(多态),所以调用的是Circle.draw()方法.

1
2
3
4
5
6
7
//: polymorphism/shape/Shape.java
package polymorphism.shape;
public class Shape {
public void draw() {}
public void erase() {}
} ///:~
1
2
3
4
5
6
7
8
//: polymorphism/shape/Circle.java
package polymorphism.shape;
import static net.mindview.util.Print.*;
public class Circle extends Shape {
public void draw() { print("Circle.draw()"); }
public void erase() { print("Circle.erase()"); }
} ///:~
1
2
3
4
5
6
7
8
//: polymorphism/shape/Square.java
package polymorphism.shape;
import static net.mindview.util.Print.*;
public class Square extends Shape {
public void draw() { print("Square.draw()"); }
public void erase() { print("Square.erase()"); }
} ///:~
1
2
3
4
5
6
7
8
//: polymorphism/shape/Triangle.java
package polymorphism.shape;
import static net.mindview.util.Print.*;
public class Triangle extends Shape {
public void draw() { print("Triangle.draw()"); }
public void erase() { print("Triangle.erase()"); }
} ///:~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//: polymorphism/shape/RandomShapeGenerator.java
// A "factory" that randomly creates shapes.
package polymorphism.shape;
import java.util.*;
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();
}
}
} ///:~
```java
//: polymorphism/Shapes.java
// Polymorphism in Java.
import polymorphism.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();
}
} /* Output:
Triangle.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Circle.draw()
*///:~

Shape基类为自它哪里继承而来的所有导出类建立了一个公用接口,所有形状都可以描绘和擦除.导出类通过覆盖这些定义,来为每种特殊类型的几何形状提供单独的行为.

RandomShapeGenerator是一种”工厂”,在我们每次调用next()方法时,它可以随机选择Shape对象产生一个引用.向上转型是在return语句里产生的.每个return语句取得一个指向某个Cricle,Square或者Triangle的引用,并将其以Shape类型从next()方法中发送出去.所以无论我们在什么时候调用next()方法,都不会知道具体类型到底是什么,因为我们总是只能获得一个通用的Shape引用.

2.3 可扩展性

因为可以从通用的基类继承出新的数据类型,从而新添加一些功能.那些操纵基类接口的方法不需要任何改动就可以应用于新类.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//: polymorphism/music3/Music3.java
// An extensible program.
package polymorphism.music3;
import polymorphism.music.Note;
import static net.mindview.util.Print.*;
class Instrument {
void play(Note n) { print("Instrument.play() " + n); }
String what() { return "Instrument"; }
void adjust() { print("Adjusting Instrument"); }
}
class Wind extends Instrument {
void play(Note n) { print("Wind.play() " + n); }
String what() { return "Wind"; }
void adjust() { print("Adjusting Wind"); }
}
class Percussion extends Instrument {
void play(Note n) { print("Percussion.play() " + n); }
String what() { return "Percussion"; }
void adjust() { print("Adjusting Percussion"); }
}
class Stringed extends Instrument {
void play(Note n) { print("Stringed.play() " + n); }
String what() { return "Stringed"; }
void adjust() { print("Adjusting Stringed"); }
}
class Brass extends Wind {
void play(Note n) { print("Brass.play() " + n); }
void adjust() { print("Adjusting Brass"); }
}
class Woodwind extends Wind {
void play(Note n) { print("Woodwind.play() " + n); }
String what() { return "Woodwind"; }
}
public class Music3 {
// Doesn't care about type, so new types
// added to the system still work right:
public static void tune(Instrument i) {
// ...
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] e) {
for(Instrument i : e)
tune(i);
}
public static void main(String[] args) {
// Upcasting during addition to the array:
Instrument[] orchestra = {
new Wind(),
new Percussion(),
new Stringed(),
new Brass(),
new Woodwind()
};
tuneAll(orchestra);
}
} /* Output:
Wind.play() MIDDLE_C
Percussion.play() MIDDLE_C
Stringed.play() MIDDLE_C
Brass.play() MIDDLE_C
Woodwind.play() MIDDLE_C
*///:~

新添加的方法what()返回一个带有类型描述的string引用;另一个新添加的方法adjust()则提供每种乐其的调用方法.

在main()中,当我们将某种引用置入orchestra 数组中,就会自动向上转型到Instrument.

多态是一项让程序员”将改变的事物与未知的事物分离开来”的重要技术.

2.4 缺陷 : “覆盖”私有方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//: polymorphism/PrivateOverride.java
// Trying to override a private method.
package polymorphism;
import static net.mindview.util.Print.*;
public class PrivateOverride {
private void f() { print("private f()"); }
public static void main(String[] args) {
PrivateOverride po = new Derived();
po.f();
}
}
class Derived extends PrivateOverride {
public void f() { print("public f()"); }
} /* Output:
private f()
*///:~

我们所期待的是输出public f(),但由于private方法被自动认为是final方法,而且对导出类是屏蔽的.因此,在这种情况下,Derived类中的f()方法就是一个全新的方法;既然基类的f()方法在子类中不可见,因此也不能被重载.

确切地说,在导出类中,对于基类的private方法,最好采用不同的名字.

2.5 缺陷 : 域与静态方法

如果你直接访问某个域,这个访问就将会在编译器进行解析.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//: polymorphism/FieldAccess.java
// Direct field access is determined at compile time.
class Super {
public int field = 0;
public int getField() { return field; }
}
class Sub extends Super {
public int field = 1;
public int getField() { return field; }
public int getSuperField() { return super.field; }
}
public class FieldAccess {
public static void main(String[] args) {
Super sup = new Sub(); // Upcast
System.out.println("sup.field = " + sup.field +
", sup.getField() = " + sup.getField());
Sub sub = new Sub();
System.out.println("sub.field = " +
sub.field + ", sub.getField() = " +
sub.getField() +
", sub.getSuperField() = " +
sub.getSuperField());
}
} /* Output:
sup.field = 0, sup.getField() = 1
sub.field = 1, sub.getField() = 1, sub.getSuperField() = 0
*///:~

当Sub对象转型为Super引用时,任何域访问都将由编译器解析,因此不是多态.在本例中,为Super.field和Sub.field分配了不同的存储空间.这样,Sub实际上包含两个称为field的域:它自己的和它从Super处得到的.然而,在引用Sub中的field时所产生的默认域并非Super版本的field域.因此,为了得到Super.field,必须显式地指明super.field.

如果某个方法是静态的,它的行为就不具有多态性,静态方法是与类,而并非与单个的对象相关联.

3. 构造器和多态

构造器并不具有多态性(它们实际上是static方法,只不过static声明是隐式的).

3.1 构造器的调用顺序

基类的构造器总是在导出类的构造过程中被调用,而且按照继承层次逐渐向上链接,已使每个基类的构造器都能得到调用.因为构造器具有一项特殊的任务:检查对象是否被正确地构造.导出类只能访问它自己的成员,不能访问基类中的成员(基类的成员通常是private类型).只有基类的构造器才具有恰当的知识和权限来对自己的元素进行初始化.因此,必须令所有构造器都得到调用.这正是编译器为什么要强制每个导出类部分都必须调用构造器的原因.在导出类的构造器主体中,如果没有明确指定调用某个基类构造器,它就会默默地调用默认构造器.如果没有明确构造器,编译器就会报错!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//: polymorphism/Sandwich.java
// Order of constructor calls.
package polymorphism;
import static net.mindview.util.Print.*;
class Meal {
Meal() { print("Meal()"); }
}
class Bread {
Bread() { print("Bread()"); }
}
class Cheese {
Cheese() { print("Cheese()"); }
}
class Lettuce {
Lettuce() { print("Lettuce()"); }
}
class Lunch extends Meal {
Lunch() { print("Lunch()"); }
}
class PortableLunch extends Lunch {
PortableLunch() { print("PortableLunch()");}
}
public class Sandwich extends PortableLunch {
private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
public Sandwich() { print("Sandwich()"); }
public static void main(String[] args) {
new Sandwich();
}
} /* Output:
Meal()
Lunch()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()
*///:~

用其他类创建了一个复杂的类,并且每个类都有一个声明它自己的构造器,其中最重要的类时Sandwich,它反映了三层继承(若将自Object的隐含继承地也算在内,就是四层)以及三个成员对象,当在main()里创建一个Sandwich对象后,就可以看到输出的结果.

  • 调用基类构造器.这个步骤还不断地反复递归下去,首先是构造这种层次结构的根,然后是下一层导出类,等等,直到最底层的导出类.
  • 按声明顺序调用成员的初始化方法.
  • 调用导出类构造器的主体.

3.2 继承和清理

通过组合和继承方法来创建新类时,永远不必担心对象的清理问题.子对象通过都会留给垃圾回收器进行处理.如果确实遇到清理的问题,那么必须用心为新类创建dispose()方法.并且由于继承的缘故,如果我们有其他作为垃圾回收器一部分的特殊清理功能,就必须在导出类中覆盖dispose()方法.当覆盖被继承的dispose()方法适,务必记住调用基类版本dispose()方法;否则,基类的清理动作就不会发生.

3.3 构造器内部的多态方法的行为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//: polymorphism/PolyConstructors.java
// Constructors and polymorphism
// don't produce what you might expect.
import static net.mindview.util.Print.*;
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
*///:~
  • 在其他任何事物发生之前,将分配给对象的存储空间初始化成二进制的零.
  • 如前所述那有调用基类构造器.此时,调用被覆盖后的draw()方法(要在调用RoundGlyph构造器之前调用),由于步骤1的缘故,我们此时会发现radius的值为0.
  • 按照声明的顺序调用成员的初始化方法.
  • 调用导出类的构造器主体.

因此,在编写构造器时有一条有效的准则:用尽可能简单的方法使对象进入正常的状态,如果也可以的情况,避免调用其它方法.

4. 协变返回类型

它表示在导出类中的被覆盖方法可以返回基类方法的返回类型的某种导出类型:

5. 用继承进行设计

用继承表达行为间的差异,并用子弹表达状态上的变化.

5.1 纯继承与扩展

纯继承与扩展

这种被称作为纯粹的”is-a”关系,因为一个类的接口已经确定了它应该是什么,继承可以确保所有的导出类具有基类的接口,且绝对不会少.

5.2 向下转型与运行时类型识别

由于向上转型(在继承层中向上移动)会丢失具体的类型信息.所以,我们通过向下转型(也就是在继承层次中向下移动),然而,我们知道向上转型是安全的,因为基类不会具有大于导出类的接口.因此,我们通过基类接口发送的信息保证都能被接受.但是对于向下转型,例如:我们无法知道一个”几何形状”它确实是一个”圆”还是”别的三角形”.

在java中,所有的转型都会被检查,以便确保它的确是我们希望的那种类型.如果不是.则返回ClassCastException(类转型异常)

6. 总结

多态意味着”不同的形式”,在面向对象的程序设计中,我们持有从基类继承而来的相同接口,以及使用该接口的不同形式:不同版本的动态绑定方法.