Class Inheritance:
The idea of inheritance is to fully leverage the functionality of existing classes and extend them to define new classes on their basis.
Parent class (parents) and child class (subclass):
The inherited class is called the parent class, and the inherited class is called a subclass, and a parent class can have more than one child class;
Subclasses, once inheriting the parent class, have the properties and methods of the parent class, which can be added and changed as needed.
One of the main benefits of this approach is code reuse.
Example code 1:
#*_*coding:utf-8*_*classPerent_class (object):def __init__(self,name): Self.name=namedefPrintname (self):Print('your name is:'+self.name)classChild_class (perent_class):def __init__(Self,name,age): Super (child_class,self).__init__(name)#initializes the class super () function to return the class objectSelf.age = Ageif __name__=="__main__": Child_class ('Xzdylyh',' -'). Printname ()
Output
C:\Python27\python.exe d:/weixin/temp/temp.py Your name is: Xzdylyhprocess finished with exit code 0
When a subclass is in the same way as a parent class, the subclass overrides the method content of the parent class, which is, in fact, a rewrite and polymorphism.
Python Basic (11) Class inheritance