码迷,mamicode.com
首页 > 其他好文 > 详细

多继承

时间:2019-08-25 18:09:41      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:dash   class   父类继承   elf   允许   ==   __init__   调用   skill   

Python允许从多个父类继承 , 称为多重继承
class A(object):
def __init__(self, a):
print ‘init A...‘
self.a = a

class B(A):
def __init__(self, a):
super(B, self).__init__(a)
print ‘init B...‘

class C(A):
def __init__(self, a):
super(C, self).__init__(a)
print ‘init C...‘

class D(B, C):
def __init__(self, a):
super(D, self).__init__(a)
print ‘init D...‘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
多重继承通过 super()调用__init__()方法时 , A 虽然被继承了两次,但__init__()只调用一次

实例
class Person(object):
pass

class Student(Person):
pass

class Teacher(Person):
pass

class SkillMixin(object):
pass

class BasketballMixin(SkillMixin):
def skill(self):
return ‘basketball‘

class FootballMixin(SkillMixin):
def skill(self):(http://www.amjmh.com/v/BIBRGZ_558768/)
return ‘football‘

class BStudent(Student, BasketballMixin):
pass

class FTeacher(Teacher, FootballMixin):
pass


if __name__ == ‘__main__‘:
s = BStudent()
print(s.skill())
t = FTeacher()
print(t.skill())
————————————————

多继承

标签:dash   class   父类继承   elf   允许   ==   __init__   调用   skill   

原文地址:https://www.cnblogs.com/ly570/p/11408527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!