The new class created by inheritance is called a subclass or derived class. The inherited class is called the base class, the parent class, or the superclass. There are 2 main implementations of the concept of inheritance: implementation inheritance, interface inheritance. 1. Implementation of inheritance refers to the ability to use the properties and methods of the base class without additional coding; 2. Interface inheritance refers to the ability to use only the names of properties and methods, but the subclass must provide the implementation (subclass refactoring method); # # The Simplest inherited class person (object): Def talk ( Self): print (' Talk 中文版 ... ') class Ypeople (person): # def talk (self): #要是没有重新定义talk会调用父类里的talk print ("Talk Chinese") def walk (self): print ("Yperson is runing ....") B=ypeople () B.talk () B.walk () # # # # # # # # # # # # # # # # # # # # # Inherit and Refactor class Person (object): Parent def __init__ (self,name,age): Self.name=name self.age=age self.sex= "Normal" Def talk (self): print (' Talk 中文版 ... ') class Bperson ( Object): Passclass ypeople (person): Def __init__ (self,name,age,strength): #先继承, re-refactor person.__init__ (self,name,age) self.www= "www" Print (self.name,self.age,self.sex) def talk (self): #要是没有重新定义talk会调用父类里的talk person.talk (self) # Inheritance method (typically not done, repeated) print ("Talk Chinese") def Walk (self): print ("Yperson is working ...") b=ypeople ("Wang Dong", "strong") B.talk () B.walk () # # Class of Inheritance Use Example Class School (object): "" "" "" "" "" "" "" "" "" "" "" "" "" Number=0 def __init__ (self,name,age,sex): self.name= Name SElf.age=age self.sex=sex Self.enroll () #在基类里内部调用 (equivalent to the subclass default call) def enroll (self): "" "Register" "Print ("%s "Welcome to our Academy!) %self.name) school.number+=1 #把值赋给公有属性 (becomes global) direct Self.number just creates a variable in the instance number=0 and then adds 1 def tell: "" "Print Your Own Information" " Print ("------info:%s-------"%self.name) for K,v in Self.__dict__.items (): print ("\ T", k,v) print ("------End-------") def __del__ (self): # destructor print ("[%s] was expelled!" "%self.name" School.number-=1class Teacher (School): "" "," "" "," "Def __init__ (self,name,age,sex,salary,course): School. __init__ (self,name,age,sex) self.salary=salary Self.coures=course def Teaching (self): print ("Teacher [%s] is teaching [ %s] "% (self.name,self.coures)) class Student (School):" "" Student Class "" "Def __init__ (self,name,age,sex,course,fee): school.__ init__ (self,name,sex,age) self.course=course self.fee=fee self.amount=0 def pay_fee (self,amount): Print ("Student [%s] has just paid [%s] "% (Self.name,self.amount)) Self.amount+=amountt1=teacher (" Wang Xiao "," f*m ", 20000," Python ") s1=student ("Ding", +, "N/A", ' Python ', 30000) S2=studenT ("Souxiao", "F", "Python", School.number) #输出总共创建次数print (t1.number) #输出t1实例本身创建次数print (school.number) # Total number of outputs created del S1 #删除s1print (school.number) #输出总共创建次数a =s1.__dict__ #把信息转换成列表print (a) T1.tell ()
Class inheritance and class inheritance use