python subclass parent class

Concise the Liezi in Python, understand and comment it yourself: for reference


class schoolmember:#parent class

    def __init__(self,name,age):#Initialize the object immediately when the object is created self.name
        =name
        self.age=age
        print('initialized schoolmember is %s' % self.name)#Note the format
    def tell(self ):#Another method
        print('name: "%s" age:%s' % (self.name,self.age))#Format
class teacher(schoolmember):#Subclass reference
    def __init__(self,name, age,salary):#Add extra elements to your own method
        schoolmember.__init__(self,name,age)#This method needs to add a reference to the parent class method, a very critical step
        self.salary=salary#Add specific content
        print(' initialized teacher is %s' % self.name)
    def tell(self):
        schoolmember.tell(self)#Ibid is very important
        print("salary:%d" % self.salary)
teacher1=teacher("li",23, 6000)#call and verify

teacher1.tell()


#The result is as follows:
initialized schoolmember is li
initialized teacher is li
name: "li" age:23
salary:6000

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326422016&siteId=291194637