为了账号安全,请及时绑定邮箱和手机立即绑定

多重继承问题: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)


正在回答

3 回答

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()



0 回复 有任何疑惑可以回复我~

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)


打印姓名没问题,打印球就报错,很好的说明了这个问题

0 回复 有任何疑惑可以回复我~

我也发现这个问题,多重继承好像是个笑话,本质上还是只继承了第一个

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

多重继承问题:super中传一个多重继承的类,只能识别到第一个

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信