This commit is contained in:
LingCoder
2020-05-25 00:29:41 +08:00
parent e2542e77d3
commit e65281627e
8 changed files with 2 additions and 0 deletions

View File

@@ -1168,6 +1168,8 @@ SadActor
纯粹的替代意味着派生类可以完美地替代基类,当使用它们时,完全不需要知道这些子类的信息。也就是说,基类可以接收任意发送给派生类的消息,因为它们具有完全相同的接口。只需将派生类向上转型,不要关注对象的具体类型。所有一切都可以通过多态处理。
![](../images/1562409366638.png)
按这种方式思考似乎只有纯粹的“is - a”关系才是唯一明智的做法其他任何设计只会导致混乱且注定失败。这其实也是个陷阱。一旦按这种方式开始思考就会转而发现继承扩展接口遗憾的是extends 关键字似乎怂恿我们这么做才是解决特定问题的完美方案。这可以称为“is - like - a” 关系,因为派生类就像是基类——它有着相同的基本接口,但还具有需要额外方法实现的其他特性:
![](../images/1562409366637.png)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 97 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 21 KiB