ipmshen
2022-04-24 15:48
问题描述:
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)
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("学生叫%s,现在读%s,考试分数为%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("老师叫{},现在在{}教{}。".format(self.name, self.gender, self.subject))
t = Teacher('xx', '一年级', '数学')
s = Student('小明', '一年级', 99, '篮球')
# t.say()
s.say()
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)
打印姓名没问题,打印球就报错,很好的说明了这个问题
我也发现这个问题,多重继承好像是个笑话,本质上还是只继承了第一个
Python3 进阶教程(新版)
41910 学习 · 236 问题
相似问题