객체 지향 프로그래밍
국제바칼로레이트 · IB Diploma · Computer Science · SL 주제 7 3:42 영어 내레이션 · 영어 + 중국어 자막 burned-in
장
전사본
A school app tracks thousands of students. Each has a name, a year group and grades, and each can enrol on a course or receive a grade.
学校应用要管理成千上万名学生,每人都有姓名、年级和成绩,都能选课或得到成绩。
Separate variables for every student would be impossible.
给每个学生单独设变量是不可能的。
Instead we write the design once, as a class, and make one object per student.
我们把设计写一次,作为一个类,再为每个学生创建一个对象。
A class is a blueprint.
类是蓝图。
Account defines two attributes, owner and balance, and methods such as deposit.
Account 类定义了两个属性:户主和余额,还有 deposit 等方法。
Each object built from it is an instance with its own state: Li's account holds fifty-five, Sam's holds twenty-five.
由它建出的每个对象都是一个实例,有自己的状态:李的账户有五十五,萨姆的有二十五。
Making an object is called instantiation.
创建对象叫做实例化。
In Python the constructor, init, runs once when an object is made and sets its starting state.
在 Python 中,构造函数 __init__ 在创建对象时运行一次,设定初始状态。
Li starts with forty and Sam with twenty.
李开始有四十,萨姆有二十。
Calling deposit fifteen on Li's account changes only Li's balance, because self is the object the method is working on.
对李的账户调用 deposit(15) 只改变李的余额,因为 self 就是方法正在处理的那个对象。
So the output is fifty-five and twenty.
所以输出是五十五和二十。
A variable holds a reference to an object, not the object itself.
变量保存的是对象的引用,而不是对象本身。
After c equals a li, both names refer to the same account.
执行 c = a_li 之后,两个名字指向同一个账户。
A deposit through c is seen through a li, because there is only one object.
通过 c 存款,通过 a_li 也能看到,因为只有一个对象。
An instance attribute, like balance, belongs to one object.
实例属性,比如余额,属于某一个对象。
A static attribute belongs to the class and is shared by every object.
静态属性属于类,由所有对象共享。
If the constructor adds one to Account dot count, then after two accounts are made, the count is two.
如果构造函数每次给 Account.count 加一,那么创建两个账户之后,count 就是二。
Encapsulation bundles the data and the methods that act on it into one class, and hides the data.
封装把数据和操作数据的方法打包进一个类,并把数据隐藏起来。
Private members can be used only by the class's own methods; in Python the name starts with an underscore.
私有成员只能被类自己的方法使用;在 Python 中名字以下划线开头。
Public members can be used by any code.
公有成员任何代码都能用。
Then the class can protect its own rules.
这样类就能保护自己的规则。
Private attributes are read through an accessor and changed through a mutator, which checks the new value first.
私有属性通过访问器读取,通过修改器改变,而修改器会先检查新值。
Li's account holds fifteen.
李的账户有十五。
Withdraw thirty is more than the balance, so it returns false and nothing changes.
取三十超过了余额,所以返回 False,什么也不变。
Withdraw fifteen is allowed, returns true, and the balance becomes zero.
取十五可以,返回 True,余额变成零。
Encapsulation gives data integrity, because invalid values are rejected in one place.
封装保证数据完整性,因为无效值在一个地方就被拒绝。
It eases maintenance, because the inside of a class can change without breaking code that uses its public methods.
它便于维护,因为类的内部可以改动,而不会破坏只使用公有方法的代码。
And it helps teamwork, because a programmer can use a class from its public interface alone.
它也有利于团队合作,因为程序员只看公有接口就能使用一个类。
To design a class from a scenario, pick out nouns that are data and verbs that are actions.
根据情境设计类时,找出表示数据的名词和表示动作的动词。
A library book has a title, an ISBN and whether it is on loan: those are private attributes.
图书馆的书有书名、ISBN 和是否已借出:这些是私有属性。
It can be lent and returned: those are public methods.
书可以借出和归还:这些是公有方法。
Lend returns false if the book is already out.
如果书已经借出,lend 就返回 False。
Object-oriented programming models real things, so designs are easy to discuss.
面向对象编程模拟真实事物,设计容易讨论。
Classes can be reused in other programs, and encapsulation protects data.
类可以在其他程序中复用,封装保护数据。
But a small program needs more code and planning, objects can use more memory, and a poor class design is hard to change later.
但小程序要多写代码、多做规划,对象可能占用更多内存,而且不好的类设计以后很难修改。
Try these. Trace what happens when two names refer to one account. Explain why the balance is private. Then use polymorphism and the is-a and has-a tests.
试试这些:追踪两个名字指向同一个账户时会发生什么;解释余额为什么是私有的;再运用多态以及“是一个”和“有一个”的判断。
In the exam, use the words exactly: a class is the blueprint, an object an instance.
考试时用词要准确:类是蓝图,对象是实例。
Draw UML with three compartments and mark each member private or public.
UML 画三个分栏,标出每个成员是私有还是公有。
Explain both halves of encapsulation, and remember that assigning an object copies the reference.
解释封装要说出两个方面,并记住给对象变量赋值只复制引用。