Pular para o conteúdo

Programação orientada a objetos

International Baccalaureate · IB Diploma · Computer Science · HL Tópico 7 3:32 Narração em inglês · Legendas em inglês + 中文 gravadas

barra de espaço play · ←/→ 5s · j/l 10s · f tela cheia · ,/. velocidade

Capítulos

Transcrição
Two bank-account model objects can respond to the same deposit operation while holding different balances. 两个银行账户模型对象可以在持有不同余额的情况下,对同一笔存款操作做出响应。
A class defines a type with state and behaviour. 类定义了一个具有状态和行为的数据类型。
An object is an instance. 对象是实例。
Encapsulation controls access through an interface so operations can preserve invariants. 封装通过接口控制访问,从而使操作能够保持不变量。
Inheritance models an appropriate type relationship; composition models an object containing or using another object. 继承建模了适当的类型关系;组合建模了一个包含或使用另一个对象的关系。
Encapsulation means controlling access to state through an interface. 封装是指通过接口控制对状态的访问。
Instance means an individual object of a class. 实例是指类的单个对象。
A method call acts on a particular instance. 方法调用作用于特定实例。
State changes should satisfy preconditions and postconditions. 状态变更应满足前置条件和后置条件。
Polymorphism lets code use a common interface with different implementations when the contract is respected. 当契约得到遵守时,多态允许代码使用通用接口配合不同的实现。
In the lab: Implement a small synthetic account or inventory model. 在实验中,实现一个小型的合成账户或库存模型。
Test two independent instances, rejected invalid operations and boundary values. 测试两个独立实例。 拒绝无效操作和边界值。
Keep the model away from real financial accounts and credentials. 确保该模型远离真实的金融账户和凭证信息。
Known: object A starts at 40 units and receives 15. Object B starts at 20 and receives 5. 已知:对象A起始为40个单位,接收15;对象B起始为20,接收5。
Final balances are 55 and 25. 最终余额分别为55和25。
Their total is 80, but neither individual object balance is 80. 两者总和为80,但任一对象的单独余额均不为80。
Your turn: An instance starts at 12 units, adds 8 and removes 3. 轮到你了:一个实例初始值为 12 个单位,加上 8,再减去 3。
Find its final value. 求其最终值。
A class is not the same thing as one instance. 类与实例并非同一概念。
Inheritance is not automatically better than composition. 继承并不自动优于组合。
Merely hiding a field does not prove that all operations preserve valid state. 仅隐藏字段并不能证明所有操作都能维持有效状态。
Not true: Two instances of a class must always share one balance. 这种说法不正确。 同一个类的两个实例并不总是共享同一个余额。
Each instance owns its own state: two accounts of the same class can hold different balances. 每个实例拥有自己的状态。 同一类的两个账户可以持有不同的余额。
They share the class definition - the operations and structure - not the field values. 它们共享类定义,即操作和结构,但不共享字段值。
A class defines a type with state and behaviour. 类定义了一种具有状态和行为的数据类型。
A method call acts on a particular instance. 方法调用作用于特定的实例。
Watch for: A class is not the same thing as one instance. 请注意:类与单个实例并非同一概念。
Each object holds its own state. 每个对象拥有自己的状态。
An operation checks its input first, so the rule the object must always obey, its invariant, still holds in the new state. 操作先检查输入,因此对象必须始终满足的规则,即不变式,在新状态中仍然成立。
Two accounts of one class can end with 55 and 25. 同一个类的两个账户可以分别是55和25。
Independent accounts start at 10 and 20. 两个独立账户的初始余额分别为 10 和 20。
Credit 5 to the first, then try to withdraw 30 under a no-overdraft rule. 向第一个账户存入 5,然后在禁止透支的规则下尝试提取 30。
Give final states and the invariant. 请给出最终状态及不变量。
States are 15 and 20. 状态为 15 和 20。
Rejected withdrawal leaves state unchanged. 被拒绝的提款操作使状态保持不变。
Instance attributes are independent; validated methods preserve balance greater than or equal to zero. 实例属性相互独立;验证通过的方法确保余额大于或等于零。
Rejection returns before mutation. 拒绝操作在变异之前返回。
With nonnegative initial balances, validated withdrawals preserve balance greater than or equal to zero. 对于非负初始余额,经过验证的提款可确保余额大于或等于零。
This small teaching class exposes attributes. 这个小型教学类暴露了属性。
A production design must control all mutation paths, not assume the method alone enforces encapsulation. 生产环境的设计必须控制所有变异路径,不能仅假设该方法能单独实现封装。
Retrieve the technical meanings, then vary one input in the original transfer and justify the model condition. 回顾相关技术术语,然后在原始迁移中改变一个输入变量,并论证模型条件的合理性。

Entrar ou criar conta

IGCSE, A-Level & AP