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

来源:3-4 Python中的多态

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)


写回答 关注

3回答

  • Dreamweaver3662984
    2022-08-10 16:01:35

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



  • dotasfans
    2022-07-02 12:14:01

    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)


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

  • dotasfans
    2022-07-02 12:12:30

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

Python3 进阶教程(新版)

学习函数式、模块和面向对象编程,掌握Python高级程序设计

41910 学习 · 236 问题

查看课程

相似问题