摘抄《Java编程思想》之多态

记录下总结部分

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

 Polymorphism means “different forms.” In object-oriented programming, you have the same interface from the base class, and different forms using that interface: the different versions of the dynamically bound methods.

 在本章中我们已经知道如果不运用数据抽象和继承,就不可能理解或者甚至不可能创建多态的例子。多态是一种不能单独看待的特性,相反它只能作为类关系“全景”中的一部分,与其他特性协同工作。

 You’ve seen in this chapter that it’s impossible to understand, or even create, an example of polymorphism without using data abstraction and inheritance. Polymorphism is a feature that cannot be viewed in isolation (like a switch statement can, for example), but instead works only in concert, as part of the larger picture of class relationships.

 为了在自己的程序中有效地运用多态乃至面向对象技术,必须扩展自己的编程视野,使其不仅包括个别类的成员和消息,而且还要包括类与类之间的共同特性以及它们之间的关系。尽管这需要极大的努力,但是这样做是非常值得的,因为它可以带来很多成效:更快的程序开发过程、更好的代码组织、更好的扩展性程序以及更容易维护的代码等。

 To use polymorphism—and thus object-oriented techniques—effectively in your programs, you must expand your view of programming to include not just members and messages of an individual class, but also the commonality among classes and their relationships with each other. Although this requires significant effort, it’s a worthy struggle. The results are faster program development, better code organization, extensible programs, and easier code maintenance.