Objects: state, behaviour and an interface
引入| English | 释义 | 拼音 |
|---|---|---|
| object/ˈɒbdʒekt/ | 对象 | duì xiàng |
| class/klæs/ | 类 | lèi |
| state/steɪt/ | 状态 | zhuàng tài |
| behaviour | 行为 | xíng wéi |
| instance/ˈɪnstəns/ | 实例 | shí lì |
| encapsulation/ɪnˌkæpsjʊˈleɪʃn/ | 封装 | fēng zhuāng |
| invariant/ɪnˈveərɪənt/ | 不变式 | bù biàn shì |
Objects for simulation, 1967
- In Oslo in the 1960s, Ole-Johan Dahl and Kristen Nygaard needed to simulate real-world systems, such as customers queuing at a counter. Their language Simula 67 let a programmer describe a class of thing — a customer — and create many objects from it, each with its own data.
- Simula introduced classes and objects; nearly every modern language follows. An object 对象 combines state 状态 (attributes) and behaviour 行为 (methods), defined by a class 类.
Which language introduced classes and objects in 1967?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Dahl and Nygaard designed it for simulation.
Class and object
- A class is the template; an object is one instance.
Accountis a class; Ana's account and Ben's account are objects. - A constructor sets the starting state.
An object is an instance of a class.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The class is the template.
Encapsulation
- Encapsulation 封装: make attributes private and allow changes only through methods. The method checks the change is valid.
- The object's invariant 不变式 — a rule that is always true, such as "balance ≥ 0" — is protected.
What does encapsulation do?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Methods validate changes.
Inheritance and polymorphism
- Inheritance:
SavingsAccount extends Accountreuses its code and adds interest. - Polymorphism: one method call,
account.monthEnd(), behaves differently for each subclass.
同一个类,各自独立的对象状态
追踪每次调用作用于哪个实例,比较被接受与被拒绝的操作,并保持两个余额非负。
Match each idea to its meaning.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Reuse and flexibility.
Worked example: an Account class
public class Account {
private double balance;
public Account(double opening) { balance = opening; }
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) { balance -= amount; return true; }
return false;
}
public double getBalance() { return balance; }
}
- Trace:
new Account(25),deposit(10),withdraw(8)→ balance 25 → 35 → 27. withdraw(100)returns false and the balance stays 27: the invariant balance ≥ 0 is preserved.
An Account starts at 25, deposits 10, then withdraws 8. What is the final balance?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
25 + 10 − 8 = 27.
What does withdraw(100) do when the balance is 27?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The invariant is preserved.
The interface
- Other code uses only the public methods — the interface. The private details can change without breaking callers.
- That is why large programs are built from objects.
A public attribute breaks encapsulation. If balance were public, any code could set it to −500 and the class could not stop it. Keep state private and validate in methods.
Making balance public keeps the invariant safe.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Any code could set it.
A class defines state and behaviour; objects are its instances. Encapsulation keeps attributes private and validates every change, protecting invariants; inheritance reuses code and polymorphism lets one call behave differently per subclass.
Design a class Thermostat with a private target temperature that must stay between 10 and 30 °C. Write its setter.
State, validation, invariant.