class person: name = 'xxp' age = 18 def intro(self): print('my name is ' + self.name) p = person() ''' pay attention to distinguish between p.intro and p.intro() ''' p.intro()
class person: ''' why use __init__(): because we could put parameter into this class ''' def __init__(self, name): self.name = name def intro(self): print('my name is ' + self.name) p = person('Alice') p.intro()
class person: def __init__(self, name): self.name = name def intro(self): print('my name is ' + self.name) class dog(person): ''' inherit the __init__() method ''' def intro(self): print('Wang wang wang ... my name is ' + self.name) p = dog('Alice') p.intro()
class person: def __init__(self, name): self.name = name def intro(self): print('my name is ' + self.name) class dog(person): def intro(self): print('Wang wang wang ... my name is ' + self.name) class cat(person): def intro(self): print('Miao miao miao ... my name is ' + self.name) def hello(person): person.intro() ''' 多态 polymorphism ''' hello(person('xxp')) hello(dog('xxp')) hello(cat('xxp'))
class person: def __init__(self, name): self.name = name def intro(self): print('my name is ' + self.name) class dog(person): def intro(self): print('Wang wang wang ...') ''' use super() to call parent's method ''' super().intro() p = dog('xxp') p.intro()
标签:__,name,python,self,person,intro,def From: https://www.cnblogs.com/daxiangcai/p/16859837.html