一、其实,“类只是一个结构”,它并不是实例化出来的对象了,只是定义,那么在定义方法的时候想要用到 super 类的方法,你就只能用 super或者用类名来调用它。
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print(self.name, '正在吃:{}'.format(food))
class YellowPeople(Person):
def __init__(self, name, age, weight):
super().__init__(name, age)#或者Person.__init__(name,age)
self.weight = weight
def print_some(self):
print(self.name, '今年{}岁,体重{}斤'.format(self.age, self.weight))
handsomeb = YellowPeople('liyuanchao', 21, 120)
handsomeb.print_some()
运行结果如下:
接下来给你讲的多继承,相信你会对 super 更近一步的认识。来举一个多继承的例子:
借用一张图
按照上图我们可以写出以下代码
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food='gaga'):
print(self.name, '正在吃:{}'.format(food))
class YellowPeopleFather(Person):
def yellow_say(self):
print('this is yellow people method')
class WhitePersonMother(Person):
def white_say(self):
print('this is white people method')
class Son(YellowPeopleFather, WhitePersonMother):
pass
test=Son('liyuanchao',18)
test.yellow_say()
test.white_say()
YellowPeopleFather(Person) 和 WhitePersonMother(Person) 是单继承,而 class Son(YellowPeopleFather, WhitePersonMother) 同时继承了 YellowPeopleFather 和 WhitePersonMother ,即就是多继承,实例化出 Son 对象的时候,可以同时拥有 YellowPeopleFather 和 WhitePersonMother 的东西,运行结果如下:
当父类都拥有共同的方法如何调用呢:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food='gaga'):
print(self.name, '正在吃:{}'.format(food))
class YellowPeopleFather(Person):
def eat(self):
print('this is yellow people method')
class WhitePersonMother(Person):
def eat(self):
print('this is white people method')
class Son(YellowPeopleFather, WhitePersonMother):
pass
test=Son('liyuanchao',18)
test.eat()
运行结果如下:
可以看到调用的是YellowPeopleFather类中的eat方法,这时候我们就要理解多重继承的子类,调用方法的顺序了,在 Python 中,使用到的是 MRO(这里不做过多解释了) ,我们可以通过调用子类的 mro 方法来具体查看,运行结果如下:
可以看到,这里是调用的顺序是:Son->YellowPeopleFather–>WhitePersonMother–>Person–>object。
- 没有在Son类中定义eat方法,所有优先在YellowPeopleFather类中寻找是否有eat方法,如果没有继续往后寻找。
其实调用 super 的时候,其实是调用 MRO 顺序的下一个类。
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food='gaga'):
print(self.name, '正在吃:{}'.format(food))
class YellowPeopleFather(Person):
def eat(self):
print('this is yellow people method')
class WhitePersonMother(Person):
def eat(self):
print('this is white people method')
class Son(YellowPeopleFather, WhitePersonMother):
def eat(self):
print('this is son method')
test=Son('liyuanchao',18)
test.eat()
很容易知道运行结果如下:
- 但是如果此时我们在Son类有eat方法时,但想调用YellowPeopleFather类的方法怎么办勒,根据 MRO 这个顺序:Son->YellowPeopleFather–>WhitePersonMother–>Person–>object。super 的下一个类就是YellowPeopleFather类
我们只需要对Son类做个小小的改动
class Son(YellowPeopleFather, WhitePersonMother):
def eat(self):
super(Son,self).eat()
运行结果如下:
- 当我们想调用WhitePersonMother类中的方法怎么做呢,其实也很简单:
class Son(YellowPeopleFather, WhitePersonMother):
def eat(self):
super(YellowPeopleFather,self).eat()
运行结果如下:
- 要用Person中的eat时,同理可得:
class Son(YellowPeopleFather, WhitePersonMother):
def eat(self):
super(WhitePersonMother,self).eat()
运行结果如下:
补充一个例子:
class A(object):
def __init__(self, params):
self.alive = params['alive']
self.ok = params['ok']
self.test = 'test{}'.format(self.alive)
def a_alive(self):
print self.alive
class B(object):
def __init__(self, params):
self.params = params
def test_params(self):
print self.params
class C(A, B):
def __init__(self, params):
super(C, self).__init__(params)
super(A, self).__init__(params)
self.name = params['name']
def your_name(self):
print self.name
print self.test
self.test_params()
test = C({'name': 'tom', 'ok': 'okok', 'alive': 'this is alive'})
test.your_name()
test.a_alive()