多重繼承問題:super中傳一個多重繼承的類,只能識別到第一個
問題描述:
B繼承A,Y繼承Z,BY繼承B和Y,那么BY中的super只能代表B(如果是YB則只能代表Y),希望是可以super可以代表BY,代碼如下:
class SkillMixin(object):
def?init(self,skillName):
self.skillName = skillName
class BasketballMixin(SkillMixin):
def?init(self,skillName):
super(BasketballMixin,self).init(skillName)
class Person(object):
def?init(self,name,gender):
self.name = name
self.gender = gender
class Student(Person):
def?init(self,name,gender,score):
super(Student, self).init(name, gender)
self.score = score
class BasketballStudent(Student,BasketballMixin,):
def?init(self,name,gender,score,skillName):
super(BasketballStudent,self).init(name,gender,score,skillName)
bs = BasketballStudent('basketboy','man',59,'basketball')
print(bs.name,bs.skillName)
2022-08-10
class Person:
? ? def __init__(self, name, gender):
? ? ? ? self.name = name
? ? ? ? self.gender = gender
class SkillMixin:
? ? def __init__(self, skill):
? ? ? ? self.skill = skill
? ? def say(self):
? ? ? ? print("技能是 %s" %(self.skill))
class BasketballMixin(SkillMixin):
? ? def __init__(self, skill):
? ? ? ? super(BasketballMixin, self).__init__(skill)
class Student(Person, BasketballMixin):
? ? def __init__(self, name, gender, score, skill):
? ? ? ? super(Student, self).__init__(name, gender)
? ? ? ? BasketballMixin.__init__(self, skill)
? ? ? ? self.score = score
? ? def say(self):
? ? ? ? print("學(xué)生叫%s,現(xiàn)在讀%s,考試分?jǐn)?shù)為%d, 喜歡%s" %(self.name, self.gender, self.score, self.skill))
? ? ? ?
class Teacher(Person):
? ? def __init__(self, name, gender, subject):
? ? ? ? super(Teacher, self).__init__(name, gender)
? ? ? ? self.subject = subject
? ? def say(self):
? ? ? ? print("老師叫{},現(xiàn)在在{}教{}。".format(self.name, self.gender, self.subject))
t = Teacher('xx', '一年級', '數(shù)學(xué)')
s = Student('小明', '一年級', 99, '籃球')
# t.say()
s.say()
2022-07-02
class Person(object):
??? def __init__(self):
??????? print("I am person")
?????? ?
class Student(Person):
??? def __init__(self):
??????? super(Student,self).__init__()
??????? print("i am student")
??????? self.name = 'Andy'
?????? ?
?????? ?
class SkillMixin(object):
??? def __init__(self):
??????? print("i can skill")
class BasketballMixin(SkillMixin):
??? def __init__(self):
??????? super(BasketballMixin,self).__init__()
??????? print("i can basketball")
??????? self.ball = "basketball"
?????? ?
?????? ?
?????? ?
class BasketballStudent(Student,BasketballMixin):
??? def __init__(self):
??????? super(BasketballStudent,self).__init__()
??????? print("student can basketball")
?????? ?
stu = BasketballStudent()
print(stu.name)
print(stu.ball)
打印姓名沒問題,打印球就報錯,很好的說明了這個問題
2022-07-02
我也發(fā)現(xiàn)這個問題,多重繼承好像是個笑話,本質(zhì)上還是只繼承了第一個