Re-Implement Rationals
: Rational Class
- Attributes:
Numerator
Denominator
- Methods:
print ( ) - should print ‘numerator / denominator’
add ( other ) - should return a new rational with addition of current and other
mul ( other ) - should return a new rational with multiplication of current and other
Inheritance
Class Relationships
: Electric-type Pokemon are simply specialized versions of regular Pokemon
Using Inheritance
- We can implement specialized classes using inheritance!
- The class definition allows us to specify that a new class inherits from a superclass:
- We call the more specialized class a subclass of the general class and the general class a superclass of the specialized class.
- The subclass inherits all class attributes of the superclass.
- The subclass can override attributes to specify how it is different from the superclass.
New dot expression rules
How to evaluate:
1. Evaluate < expression >, which yields an object.
2. < name > is matched against the instance attributes of that object; if an attribute with that name exists, its value is returned.
3. If not, the name is looked up in the class, which yields a class attribute value. If it is not found in the class, look in any superclasses.
4. That value is returned unless it is a function, in which case a bound method is returned instead.
Designing for inheritance
- don't repeat yourself; use existing implementations
- attributes that have been overridden are still accessible via class objects
- look up attributes on instances when possible to allow for more specialization
: reuse the general attack instead of rewriting code
: use self.prob over ElectricType.prob
'University of California, Berkeley > ElectricalEngineering & ComputerSciences' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Interfaces (0) | 2019.07.26 |
---|---|
Linked Lists & Mutable Trees (0) | 2019.07.26 |
Objects (0) | 2019.07.23 |
Week 5 (0) | 2019.07.23 |
(Higher-Order) Functions, Sequences, Recursion and TreeRecursion, Trees (0) | 2019.07.18 |