>>     < >




In-class notes for 04/18/2014

CS 121B (CS1), Spring 2014

  • Homework assignment

  • 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:

    1. A new object of that type is allocated in memory.

    2. The state variables of that new object are assigned values, by calling __init__() and passing the constructor args.

    3. 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.

  • Corrected specs

Inheritance

  • Consider a spec for an InterestAccount class, or a TradeBook class, or a Tree 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 for IntAccount
  • Python implementation of inheritance:

    • To specify a superclass:

          class Subcl(Supercl):
      

    • To call the superclass's __init__() within the subclass's

    • To 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




< >