Object-Oriented Programming
University Computer Science Topic 4 5:16 English narration · English and 中文 subtitles you can switch
Chapters
Transcript
This course is about how a small script grows into a system that other people can safely change.
这门课讲的是一个小脚本如何成长为一个其他人可以安全修改的系统。
The first two units build the object: classes, references and the wall of encapsulation.
前两个单元构建对象:类、引用和封装的保护墙。
The next two connect objects through composition, inheritance and interfaces.
接下来的两个单元通过组合、继承和接口将对象连接起来。
The last two keep the system alive with exceptions and tests.
最后两个单元用异常和测试维持系统的运行。
Every class is a contract with its callers.
每个类都是它与调用者之间的一份契约。
A class is a blueprint, and an object is one thing built from it.
类是一张蓝图,对象是根据蓝图创建的一个具体实例。
The constructor sets each object's attributes.
构造函数设置每个对象的属性。
Attribute lookup walks outward: the instance first, then the class, then its bases.
查找属性时会逐层向外查找:先查实例,再查类,最后查基类。
Assignment only adds a name: after c equals a, both names point to one book, so taking a copy through a shows up through c.
赋值只是增加一个名称:在 c 等于 a 之后,两个名称指向同一本书,所以通过 a 借出一本书时,通过 c 也能看到这一变化。
In this reference graph, the names a and c are two arrows pointing at the same Book object, while b points to a second, separate Book.
在这张引用图中,名称 a 和 c 是指向同一个 Book 对象的两支箭头,而 b 指向另一个独立的 Book 对象。
Change the object through a, and c sees the change too, because there is only one object.
通过 a 修改对象,c 也能看到变化,因为对象只有一个。
Equality compares values; identity asks whether two names point to the very same object.
相等性比较的是值;同一性判断的是两个名称是否指向同一个对象。
Encapsulation protects an invariant: a copy is either out to exactly one borrower or not out at all.
封装保护一个不变量:一本书要么恰好借给一位借阅者,要么没有借出。
If every change goes through the issue and give-back methods, two checks are enough to guarantee it.
如果所有修改都通过借出和归还方法进行,只需两项检查就足以保证这一点。
But if callers can set the flag directly, they can create states no method would allow.
但如果调用者可以直接设置标志,就能创建出任何方法都不允许的状态。
Expose behaviour, not storage, and return copies.
应当暴露行为,而不是内部存储,并返回副本。
The public methods form a wall, with checked doors.
公共方法构成了一堵墙,墙上有经过检查的门。
Every change to the object's state must pass through a door, where the checks run.
对象状态的每一次改变都必须经过一扇门,在那里执行检查。
The fields behind the wall are private, marked with an underscore in Python.
墙后的字段是私有的,在 Python 中用下划线标记。
If you let callers reach the fields directly, every one of them becomes a place to repair later.
如果让调用者直接访问字段,那么每一个这样的访问位置都可能成为将来需要修复的地方。
Inheritance says is-a: a Member is a Person, and its constructor calls the base constructor first, through super.
继承表示 是一种:Member 是一种 Person,它的构造函数先通过 super 调用基类构造函数。
Composition says has-a: a Library has a Catalog and asks it questions.
组合表示 拥有:Library 拥有一个 Catalog,并向它查询。
With five copies added and two issued, available reports three.
添加五本书并借出两本后,available 返回三。
When overriding a method, call super and adjust it, rather than copying the base method's body.
重写方法时,应调用 super 再进行调整,而不是复制基类方法的方法体。
This class diagram shows the two kinds of connection.
这张类图展示了两种连接方式。
A hollow triangle points from a subclass to its base: inheritance, is-a.
空心三角形从子类指向基类:表示继承,即“是一种”。
A filled diamond joins a whole to its part: composition, has-a.
实心菱形连接整体与部分:表示组合,即“有一个”。
Choose each connection with a reason you can say out loud.
选择每种连接方式时,都要有一个能说清楚的理由。
Choosing wrong leaves you with a fragile base class that breaks its children.
选错会导致基类变得脆弱,进而破坏它的子类。
An interface names a behaviour, not a class.
接口规定的是一种行为,而不是一个类。
In Python, duck typing accepts any object with the right methods, and a protocol writes that contract down.
在 Python 中,鸭子类型接受任何具有正确方法的对象,而协议将这份契约明确写下来。
One loop over three senders prints email, text and queue messages, and the loop never knows the concrete classes.
一个循环遍历三个发送者,输出电子邮件、短信和队列消息,而循环始终不需要知道具体的类。
A substitute may accept more than promised, but must never break a promise.
替代对象可以接受比契约承诺的更多,但绝不能违背承诺。
Follow one call in three steps.
分三步跟踪一次调用。
The variable's annotation names only the interface.
变量的类型注解只指定接口。
The object itself carries its real runtime type.
对象自身带有它真正的运行时类型。
Dispatch then picks the send method belonging to that type.
随后,动态分派会选择属于该类型的 send 方法。
So the annotation does not choose the behaviour; the object does.
所以决定行为的不是类型注解,而是对象。
New types can be added without editing the loop.
添加新类型时无需修改循环。
A small hierarchy of exceptions lets each caller choose how precisely to handle errors: a general loan error at the base, with renewal and limit errors beneath it.
一个简单的异常层次结构让每个调用者都能选择处理错误的精确程度:基类是通用借阅错误,下面是续借错误和借阅限额错误。
When renewing a 20-day loan by 15 days breaks the 30-day limit, the error travels outwards, running each finally block, until a handler catches it.
当一笔 20 天的借阅续借 15 天会超过 30 天的限制时,异常向外传播,依次执行各个 finally 块,直到被处理程序捕获。
The loan still reads 20.
借阅期限仍然是 20 天。
Watch the exception travel.
看看异常是怎样传播的。
It is raised inside renew, unwinds through issue and main, and stops at the first handler that matches its type.
它在 renew 内部被抛出,经过 issue 和 main 逐层展开调用栈,在第一个类型匹配的处理程序处停下。
On the way, every finally block runs, on every path, so a lock is always released.
在传播途中,无论走哪条路径,每个 finally 块都会执行,因此锁总会被释放。
A returned error code like minus one can be ignored; an exception cannot.
像负 1 这样的返回错误码可以被忽略;异常则不能。
Each test follows arrange, act, assert, and checks one behaviour.
每个测试都遵循准备、执行、断言的步骤,并检查一种行为。
Renewing within the limit gives 27 days; exactly 30 is allowed; beyond 30 raises an error and leaves the loan unchanged.
在限制内续借得到 27 天;恰好 30 天也是允许的;超过 30 天则会抛出错误,并保持借阅期限不变。
Once the tests are green, you can refactor safely: moving fines into their own calculator keeps all three tests passing, with less coupling.
测试全部通过后,就可以安全地重构:把罚款计算移到独立的计算器中,三个测试仍然全部通过,同时降低了耦合。
In this coverage grid, the behaviours run down the side and the tests run across the top.
在这张覆盖网格中,行为沿左侧向下排列,测试沿顶部横向排列。
Each filled cell is a behaviour that a test pins in place.
每个填满的单元格都表示一项被某个测试固定下来的行为。
The empty cell shows exactly which test to write next.
空单元格明确指出了接下来要写哪个测试。
Naming tests after behaviours, not method names, means they survive when methods are renamed.
用行为而不是方法名来命名测试,意味着方法改名时测试仍然有效。
Now test yourself from memory.
现在凭记忆自测一下。
After c equals a, how many objects do the names a and c refer to?
在 c 等于 a 之后,名称 a 和 c 引用了多少个对象?
What happens if you issue a copy that is already out?
如果借出一本已经借出的书,会发生什么?
When does the base class constructor run?
基类构造函数什么时候运行?
When does a finally block run?
finally 块什么时候运行?
Pause, answer each one, then find it on the figure.
暂停,逐一回答,然后在图中找到答案。
Comments
No comments yet. Be the first to ask or share something.