Notes about classes
#-*-Encoding: UTF-8-*-# This is an example of classes, subclasses, and inheritance on page 330 of this book. Some annotations are added. # ---- (1) define classes (and methods) class AddrBookEntry (object): # defines the class AddrBookEntry. The class name must start with a common uppercase letter # ||# keyword | parameter. If no parent class (base class) exists ), here (object) can be omitted # class name 'address book entry class address book entry class' # function or class document string def _ init _ (self, nm, ph ): # define the constructor, __init _ () is a special method # | # keyword self parameter, all method declarations exist # define a method _ init _ (), that is, add the function self to the class. name = nm # Set name. nm is the property self of this class. phone = ph # Set phone. ph is the property of this class. print 'created instance for: ', self. name def updatePhone (self, newph): # define method updatePhone () # | # keyword self parameter, all method declarations contain self. phone = newph print 'updated phone # for: ', self. name # ----------- the above are the definition classes and methods, the following is the creation of an instance, also known as instantiation --------------- # ---- (2) create an instance john = AddrBookEntry ('John Doe ', '1970-555-1212 ') # create an instance for John Doe. jane = AddrBookEntry ('Jane Doe', '1970-555-1212 ') # create an instance for Jane Doe print "John's phone #", john. phone # ---- (3) use this instance to call john. updatePhone ('1970-555-1212 ') print "John's new phone #:", john. phone # If john. phone, you need to access the instance attributes # ----------- the following is to create a subclass (derived from) by inheritance, the subclass will keep all the features (attributes) of the parent class (base class) ---------- # ---- (a) defines the subclass (and method) class EmpAddrBookEntry (AddrBookEntry): # | # The parameter in the subclass Declaration uses the parent class (base class) Name, it is no longer the object 'employee Address Book Entry class Employee Address class' # The document string def _ init _ (self, nm, ph, id, em) of the function or class ): # define the constructor, __init _ () is a special method # | # keyword self parameter, all method declarations exist # define a method _ init _ (), that is, add the function AddrBookEntry to the subclass. _ init _ (self, nm, ph) # The constructor of the parent class (base class) must write self. empid = id self. email = em def updateEmail (self, newem): # define the method updateEmail () self. email = newem print 'updated E-mail address for: ', self. name # ---- (B) Create a subclass instance john = EmpAddrBookEntry ('John Doe ', '2017-555-1212', 42, 'John @ spam. doe ') # create an instance for John Doe print john. empid, john. name, ':', john. phone, john. email # ---- (c) use this subclass instance to call the subclass method john. updateEmail ('John @ doe. spam ') print john. empid, john. name, ':', john. phone, john. email # ---- (d) Use This subclass instance to call the base class (parent class) method john. updatePhone ('1970-999-1243 ') print john. empid, john. name, ':', john. phone, john. email
Running result
Created instance for: John Doe
Created instance for: Jane Doe
John's phone #408-555-1212
Updated phone # for: John Doe
John's new phone #: 415-555-1212
Created instance for: John Doe
42 John Doe: 408-555-1212 john@spam.doe
Updated E-mail address for: John Doe
42 John Doe: 408-555-1212 john@doe.spam
Updated phone # for: John Doe
42 John Doe: 555-999-1243 john@doe.spam