I knew Python early, but didn't insist on learning. Recent whim, but can make up for this regret.
Object several important concepts:
Polymorphic: The same operation can be used for objects of different classes;
Encapsulation: Working details of hidden objects in the external world;
Inheritance: A specialized class object is built on the basis of a common class.
Examples of inheritance:
Class Addrbookentry (object):
' Address Book entry class '
def __init__ (self, NM, ph):
Self.name = NM
Self.phone = ph
print ' created instance for: ', self.name
def updatephone (self, newph):
Self.phone = newph
print ' Updated phone# for: ', self.name
Sub-class:
Class Empladdrbookentry (Addrbookentry):
' Employee Address Book entry class '
def __init__ (self, NM, ph, id, EM):
Addrbookentry.__init__ (self, NM, ph)
Self.empid = ID
Self.email = em
def updateemail (self, NEWEM):
Self.email = Newem
print ' Updated e-mail address for: ', Self.email
Polymorphic examples
Polymorphism is a property that appears after the instantiation of a class, meaning that even if you do not know what type of object is being referenced, you can manipulate it, and it will behave differently depending on the type of object.
In the inheritance process, it is more recessive that the function interface of the parent class will be overwritten when the subclass redefine the functional interface of the parent class.
Class Human (object):
def run (self):
print ' Human is running ... '
Class Man (Human):
Pass
Class Woman (Human):
Pass
John = Man ()
Jude = Woman ()
John.run ()
Jude.run ()
Class Man1 (Human):
def run (self):
print ' Man is running ... '
Pass
Class Woman1 (Human):
def run (self):
print ' Woman is running ... '
Pass
John = Man1 ()
Jude = Woman1 ()
John.run ()
Jude.run ()
The abstraction of the basic Python tutorial