问答详情
源自:3-4 Python中的多态

不知道哪里错了,请大神指导

class Person:
    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 Teacher(Person):
    def __init__(self,name,gender,subject):
        super(Teacher, self).__init__(name, gender)
        self.subject=subject

class SkillMixin:
    def __init__(self,skill):
        self.skill=skill
class BasketballMixin(SkillMixin):
    def __init__(self,skill,basketball):
        super(BasketballMixin, self).__init__(skill)
        self.basketball=basketball

class FootballMixin(SkillMixin):
    def __init__(self,skill,football):
        super(FootballMixin, self).__init__(skill)
        self.football=football

class BasStudent(Student,BasketballMixin):
    def __init__(self,name,gender,score,skill,basketball):
        super(BasStudent, self).__init__(name,gender,score)
    def getskill(self):
        print("我叫 %s,我会打%s "%(self.name,self.basketball))
a=Student('jiji','boy',13)
b=BasketballMixin('high','篮球',)
c=BasStudent.getskill()
print(c)


提问者:高飞的鱼 2022-03-04 21:57

个回答

  • 慕前端7080484
    2022-03-06 18:14:50

    class BasStudent(Student,BasketballMixin):

        def __init__(self,name,gender,score,skill,basketball):

            super(BasStudent, self).__init__(name,gender,score)

        def getskill(n,k):

            print("我叫 %s,我会打%s "%(n,k))

    a=Student('jiji','boy',13)

    b=BasketballMixin('high','篮球',)

    c=BasStudent.getskill(a.name,b.basketball)


    --我改成这个样子,好像能得到题主想要的结果。其原因在于如果用getskill(self),因为有两个父类,self无法区分(或者我还不知道该怎样区分)student或basketballmixin。然后我就偷懒这样改了。😀

    下载视频