In-class notes for 04/18/2014
CS 121B (CS1), Spring 2014
Quiz
Project planning -- next week
Remaining new topics: more on objects/classes; simulation
Submitted questions on assignments and technology
Upcoming
Submitted questions on readings
Steps taken by Python during constructor calls.
Python objects are created by calling the name of their class as a function, and providing arguments for
__init__()
.Example:
acct = Acct(450)
When a constructor is called, three steps occur:
A new object of that type is allocated in memory.
The state variables of that new object are assigned values, by calling
__init__()
and passing the constructor args.That newly allocated object is returned.
Python automatically carries out steps 1 and 3. A program affects the process by defining a method
__init__()
whose steps are carried out in step 2.
Inheritance
Consider a spec for an
InterestAccount
class, or aTradeBook
class, or aTree
class...Inheritance: when one class automatically receives (inherits) the state variables and methods of another.
Superclass: A class that provides inherited state variables and methods
Subclass: A class that receives (obtains?) inherited state variables and methods.
Spec forIntAccount
Python implementation of inheritance:
To specify a superclass:
class Subcl(Supercl):
To call the superclass's
__init__()
within the subclass'sTo call a superclass's method, use
super()
, e.g.,super().getBalance()
____________
______
______
______
______
______
______
____________
To call a superclass method that has the same name as a subclass method:
Supercl.meth(self, args)
Calling superclass constructor with
super()
def __init__(self, ...): super(self, ...)
Relationships between classes
< >