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

来源:3-4 Python中的多态

慕无忌6336060

2021-04-02 17:22

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)


写回答 关注

4回答

  • 方木学长
    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)


    高飞的鱼

    这个代码在我的编辑器里,朋友可以帮忙看看哪里不对吗?报错信息: Traceback (most recent call last): File "/Users/lihui/IdeaProjects/laern/Python j进阶/3章 类的继承.py", line 270, in <module> c=BasStudent.getskill() TypeError: getskill() missing 1 required positional argument: 'self'

    2022-03-05 08:32:19

    共 1 条回复 >

  • 慕无忌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


Python3 进阶教程(新版)

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

41910 学习 · 236 问题

查看课程

相似问题