#InheritanceclassPerson :defEat (self):Print("Eating ...") defRun (self):Print("runing ...")#inherited SyntaxclassStudent (person):defStudy (self):Print("study ...") STU1=Student () stu1.run ( )
#rewriteclassPerson :defEat (self):Print("Eating ...") defRun (self):Print("runing ...")#subclasses overriding parent class methodsclassStudent (person):defRun (self):Print("Quick runing ...") Print("father Func") #calling a parent class method in a subclass #第一种方法: Note that you need to pass the argument self at this timePerson.run (self)#second method: Calling the parent class through the super () methodsuper (). Run ()defStudy (self):Print("study ...") STU1=Student () stu1.run ( )
#the inheritance of private methods or private properties in a classclassPerson :def __init__(self): Self.name="Tom"Self .__age= 14def __gettom(self):Print(self.)__age)classStudent (person):defShow (self):#child class cannot inherit private member property of parent class #Print ("name is%s, and is%d."% ( Self.name,self.__age)) Print("name is%s"%(self.name))defShowtom (self):#subclasses cannot inherit private methods of parent class #__gettom ()Stu=Student () stu.showtom ( )
Inheritance of Python classes