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

类的多重调用怎么报错,如下错误:

class person(object):

    def __init__(self,name,age,gender):

        self.name=name

        self.age=age

        self.gender=gender

class skillmixin(object):

    def __init__(self,skillname):

        self.skillname=skillname

        

class student(person):

    def __init__(self,name,age,gender,score):

       super(student,self).__init__(name,age,gender)

       self.score=score

class teacher(person):

    def __init__(self,name,age,gender,coures):

        super(teacher,self).__init__(name,age,gender)

        self.coures=coures

        

class basketballmixin(skillmixin):

    def __init__(self,skillname,basketballlevel):

        super(basketballmixin,self).__init__(skillname)

        self.basketballlevel=basketballlevel

class footballmixin(skillmixin):

    def __init__(self,skillname,footballlevel):

        super(footballmixin,self).__init__(skillname)

        self.footballlevel=footballlevel        


class studenthobby(student,basketballmixin):

    def __init__(self,skillname,basketballlevel):

        super(studenthobby,self).__init__(name,age,gender,score,skillname,basketballlevel)

        print('I am a student being good at playing basketball,my name is %s'%s,self.name,self.basktballlevel)

        

class teacherhobby(teacher,footballmixin):

    def __init__(self,skillname,footballlevel):

        super(teacherhobby,self).__init__(name,age,gender,coures,skillname,footballlevel)

        print('I am a teacher being good at playing football,my name is %s'%s,self.name,self.footballlevel)


s=student('liyawang',15,'female',90)

t=teacher('jake',48,'male','comprehensive')

sk=basketballmixin('basketball','junior')

tk=footballmixin('foottball','medium')

#sh=basketballmixin(sk,'junior')

#th=footballmixin(tk,'medium')

studenthobby(s,sk)

#teacherbobby(t,th)


提问者:慕无忌6336060 2021-04-02 17:22

个回答

  • 方木学长
    2021-04-10 22:31:02

    class person(object):
        def __init__(self, name, age, gender):
            self.name = name
            self.age = age
            self.gender = gender
    
    class skillmixin(object):
        def __init__(self, skillname):
            self.skillname = skillname
    
    class student(person):
        def __init__(self, name, age, gender, score):
            super(student, self).__init__(name, age, gender)
            self.score = score
    
    class teacher(person):
        def __init__(self, name, age, gender, coures):
            super(teacher, self).__init__(name, age, gender)
            self.coures = coures
    
    class basketballmixin(skillmixin):
        def __init__(self, skillname, basketballlevel):
            super(basketballmixin, self).__init__(skillname)
            self.basketballlevel = basketballlevel
    
    class footballmixin(skillmixin):
        def __init__(self, skillname, footballlevel):
            super(footballmixin, self).__init__(skillname)
            self.footballlevel = footballlevel
    
    class studenthobby(student, basketballmixin):
        def __init__(self, name, age, gender, score,skillname, basketballlevel):
            super(studenthobby, self).__init__(name, age, gender, score)
            print('I am a student being good at playing basketball,my name is %s' % self.name)
    
    class teacherhobby(teacher, footballmixin):
        def __init__(self, name, age, gender, coures,skillname, footballlevel):
            super(teacherhobby, self).__init__(name, age, gender, coures)
            print('I am a teacher being good at playing football,my name is %s' % self.name)
    
    s = student('liyawang', 15, 'female', 90)
    t = teacher('jake', 48, 'male', 'comprehensive')
    sk = basketballmixin('basketball', 'junior')
    tk = footballmixin('foottball', 'medium')
    # sh=basketballmixin(sk,'junior')
    # th=footballmixin(tk,'medium')
    studenthobby(s.name,s.age,s.gender,s.score,sk.skillname,sk.basketballlevel)
    # teacherbobby(t,th)


  • 方木学长
    2021-04-10 22:15:58

    class person(object):
        def __init__(self, name, age, gender):
            self.name = name
            self.age = age
            self.gender = gender
    
    class skillmixin(object):
        def __init__(self, skillname):
            self.skillname = skillname
    
    class student(person):
        def __init__(self, name, age, gender, score):
            super(student, self).__init__(name, age, gender)
            self.score = score
    
    class teacher(person):
        def __init__(self, name, age, gender, coures):
            super(teacher, self).__init__(name, age, gender)
            self.coures = coures
    
    class basketballmixin(skillmixin):
        def __init__(self, skillname, basketballlevel):
            super(basketballmixin, self).__init__(skillname)
            self.basketballlevel = basketballlevel
    
    class footballmixin(skillmixin):
        def __init__(self, skillname, footballlevel):
            super(footballmixin, self).__init__(skillname)
            self.footballlevel = footballlevel
    
    class studenthobby(student, basketballmixin):
        def __init__(self, name,age,gender,score,skillname, basketballlevel):
            student.__init__(self,name,age,gender,score)
            basketballmixin.__init__(self,skillname, basketballlevel)
            print('I am a student being good at playing %s,my name is %s' %(self.name,self.basketballlevel))
    
    class teacherhobby(teacher, footballmixin):
        def __init__(self, name,age,gender,coures,skillname, footballlevel):
            teacher.__init__(self,name, age, gender, coures )
            footballmixin.__init__(self,skillname, footballlevel)
            print('I am a teacher being good at playing %s,my name is %s' % (self.name, self.footballlevel))
    
    s = student('liyawang', 15, 'female', 90)
    t = teacher('jake', 48, 'male', 'comprehensive')
    sk = basketballmixin('basketball', 'junior')
    tk = footballmixin('foottball', 'medium')
    # studenthobby(s.name, s.gender,s.score,tk.skillname,sk.basketballlevel)
    # studenthobby("小明",12,"gender",90,"skillname",1)
    studenthobby(s.name,s.age,s.gender,s.score,sk.skillname,sk.basketballlevel)
    # sh=basketballmixin(sk,'junior')
    # th=footballmixin(tk,'medium')
    # teacherbobby(t,th)


  • 慕无忌6336060
    2021-04-02 17:23:51

    请盆友答复,谢谢!

  • 慕无忌6336060
    2021-04-02 17:22:52

    ile "index.py", line 45, in 
        studenthobby(s,sk)
      File "index.py", line 31, in __init__
        super(studenthobby,self).__init__(name,age,gender,score,skillname,basketballlevel)
    NameError: global name 'name' is not defined