Fig. 4 Pure Substitution
That is, the base class can receive any message you can send to the derived class because the two have exactly the same interface. All you need to do is upcast from the derived class and never look back to see what exact type of object you’re dealing with. Everything is handled through polymorphism.
When you see it this way, it seems like a pure is-a relationship is the only sensible way to do things, and any other design indicates muddled thinking and is by definition broken. This too is a trap. As soon as you start thinking this way, you’ll turn around and discover that extending the interface (which, unfortunately, the keyword extends seems to encourage) is the perfect solution to a particular problem. This could be termed an “is-like-a” relationship, because the derived class is like the base class—it has the same fundamental interface—but it has other features that require additional methods to implement:
Fig. 5 Implement Interface
While this is also a useful and sensible approach (depending on the situation), it has a drawback. The extended part of the interface in the derived class is not available from the base class, so once you upcast, you can’t call the new methods:
Fig. 6 Deliver Message
If you’re not upcasting in this case, it won’t bother you, but often you’ll get into a situation in which you need to rediscover the exact type of the object so you can access the extended methods of that type. The following section shows how this is done.
B.原文的翻译
多形性对于一个面向对象的语言来说,是继数据的抽象化和继承之后最为重要的特点。论文网
它提供了另一种方式去分离本来用户想用与继承的接口,而且它让从什么到怎么的过程中更加的低耦合,多形性允许改善代码的组织性和稳定性,就好像让一个可扩展的程序变得拥有了成长性,而且这种的成长性的产生不仅仅使用与程序的开头,当新的特征出现的之后它也是可以实现这种功能的。
- 上一篇:电子商务英文文献和中文翻译
- 下一篇:搅拌釜内混合液体的分离涡模拟英文文献和中文翻译
-
-
-
-
-
-
-
现代简约美式风格在室内家装中的运用
高警觉工作人群的元情绪...
NFC协议物理层的软件实现+文献综述
上市公司股权结构对经营绩效的影响研究
浅析中国古代宗法制度
巴金《激流三部曲》高觉新的悲剧命运
C++最短路径算法研究和程序设计
g-C3N4光催化剂的制备和光催化性能研究
江苏省某高中学生体质现状的调查研究
中国传统元素在游戏角色...