Inheritance: Is the relationship between classes and classes in Python, mainly using classes that have already been written to produce new classes, and subclasses inherit properties or methods from the parent class!
1 #-*-coding:utf-8-*-2 classPerson :3 def __init__(self,race,age,height,sex):4Self.race =Race5Self.age = Age6Self.height =Height7Self.sex =Sex8 defTalk (self,language):9Self.language =languageTen ifLanguage = ='中文版': One Print 'I am seapking%s'%Self.language A Else: - Print "I don ' t konw i ' m speaking what language now!" - the - classPersondemo (person):#Inherit parent class - def __init__(self,name,country,like,race,age,height,sex): -Person.__init__(Self,race,age,height,sex)#inheriting parent class properties +Self.name =name -Self.country =Country +Self.likes = like A at definfos (self): - Print " "This is my informaion: - Name:%s - Age:%s - Skin Tone:%s - Height:%s in Gender:%s - Nationality:%s to Hobbies:%s + " "%(Self.name,self.age,self.race,self.height,self.sex,self.country,self.likes) - the #instantiating an object *p = Persondemo ('Dante','CN',' Games','Yellow', 27,177,'male') $ #methods that call the subclass itselfPanax Notoginseng P.infos () - #invoking a method inherited from a parent class theP.talk ('中文版')
Results obtained:
is my informaion: name: Dante interracial: Yellow 177 Sex: Male Nationality: CN Hobby: Games I am seapking 中文版
Writing a subclass only requires it to specify its parent class, and the rest is basically similar to the normal class
Inheritance in Python