This is the lecture note of CS61A - Lecture 17.
Attribute Assignment
- Instance attribute: attribute of an instance
- Class attribute: attribute of the class of an instance
Attribute assignment statements change the values that are bound with an object or a class through attribute names.
1 | # Code Review |
Inheritance
Base class attributes aren't copy into subclasses! Instead, it is a process of looking up an attribute by name.
- If it names an attribute in the class, return the attribute value.
- Otherwise, look up the name in the base class, if there is one.
1 | ch = CheckingAccount('Tom') # Calls Account.__init__ |
Object-Oriented Design
Design guidance:
- Don't repeat yourself, use existing implementations
- Attributes that have been overridden are still accessible via class objects
- Look up attributes on instances whenever possible
- Inheritance is best for representing is-a relationships:
- Eg: a checking account is a specific type of account. So,
CheckingAccount
inherits fromAccount
- Eg: a checking account is a specific type of account. So,
- Composition is best for representing has-a relationships:
- composition: one object has another object as an attribute
- Eg: a bank has a collection of bank accounts it manages. So, a bank has a list of accounts as an attribute
1 | # DEMO |
Attributes Lookup Practice
There is a good example to check your understanding of looking up attributes.
🚀 LINK: Hint Video
Multiple Inheritance
Multiple inheritance is when a subclass has multiple base classes.
1 | 'John') such_a_deal = AsSeenOnTVAccount( |