Skip to content

Commit 25966b0

Browse files
committed
Liskov Substitution Principle
1 parent 510212d commit 25966b0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

README-zh-CN.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,21 +1235,29 @@ class HttpRequester {
12351235
**[⬆ 返回顶部](#代码整洁的-javascript)**
12361236

12371237

1238-
### Liskov Substitution Principle (LSP)
1238+
### 里氏代换原则 (LSP)
12391239
This is a scary term for a very simple concept. It's formally defined as "If S
12401240
is a subtype of T, then objects of type T may be replaced with objects of type S
12411241
(i.e., objects of type S may substitute objects of type T) without altering any
12421242
of the desirable properties of that program (correctness, task performed,
12431243
etc.)." That's an even scarier definition.
12441244

1245+
这是针对一个非常简单的里面的一个恐怖意图, 它的正式定义是: “如果 S 是 T 的一个子类型, 那么类
1246+
型为 T 的对象可以被类型为 S 的对象替换(例如, 类型为 S 的对象可作为类型为 T 的替代品)儿不需
1247+
要修改目标程序的期望性质 (正确性、 任务执行性等)。” 这甚至是个恐怖的定义。
1248+
12451249
The best explanation for this is if you have a parent class and a child class,
12461250
then the base class and child class can be used interchangeably without getting
12471251
incorrect results. This might still be confusing, so let's take a look at the
12481252
classic Square-Rectangle example. Mathematically, a square is a rectangle, but
12491253
if you model it using the "is-a" relationship via inheritance, you quickly
12501254
get into trouble.
12511255

1252-
**Bad:**
1256+
最好的解释是, 如果你又一个基类和一个子类, 那个基类和字类可以互换而不会产生不正确的结果。 这可
1257+
能还有有些疑惑, 让我们来看一下这个经典的正方形与矩形的例子。 从数学上说, 一个正方形是一个矩形,
1258+
但是你用 "is-a" 的关系用继承来实现, 你将很快遇到麻烦。
1259+
1260+
**不好的:**
12531261
```javascript
12541262
class Rectangle {
12551263
constructor() {
@@ -1303,7 +1311,7 @@ const rectangles = [new Rectangle(), new Rectangle(), new Square()];
13031311
renderLargeRectangles(rectangles);
13041312
```
13051313

1306-
**Good**:
1314+
**好的:**
13071315
```javascript
13081316
class Shape {
13091317
setColor(color) {

0 commit comments

Comments
 (0)