Objects: state, behaviour and an interface
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| instance/ˈɪnstəns/ | thực thể |
| encapsulation/ɪnˌkæpsjʊˈleɪʃn/ | bao封装 |
What would explain this observation?
- Two bank-account model objects can respond to the same deposit operation while holding different balances.
Build the model
- 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: Controlling access to state through an interface.
- instance: An individual object of a class.
What should prevent an invalid state change?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Encapsulation works when every state change passes through an operation that checks the invariant before applying it. Publicly writable fields bypass the check entirely, and users will eventually make errors.
Match each technical term to its precise meaning.
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Use the definitions to distinguish related quantities and processes.
Choose evidence that can test it
- 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.
Which two habits make the investigation or model in this case more defensible?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Stating the conditions and assumptions lets others repeat the work exactly, and recording the supporting evidence lets them check the conclusion. Hiding disagreeing results removes the evidence that could challenge a wrong conclusion, and one measurement can never establish a wider claim.
Work from known quantities
- Known: object A starts at 40 units and receives 15; object B starts at 20 and receives 5.
- Final balances are 55 and 25.
- Their total is 80, but neither individual object balance is 80.
Your turn: An instance starts at 12 units, adds 8 and removes 3. Find its final value.
An instance starts at 12 units, adds 8 and removes 3. Find its final value.
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
The result is 17 units. Known: start 12; add 8; remove 3. Track the state step by step: 12 + 8 = 20; 20 - 3 = 17 units. Each operation changes only this instance's state.
Check the conclusion and its limits
- 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.
Two instances of a class must always share one balance.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
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.
Controlling access to state through an interface: write the technical term.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
encapsulation means Controlling access to state through an interface.