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

求帮找代码异常打印输出

# Enter a code
class Person(object):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender
    def who(self):
        return 'I am a Person, my name is %s' % self.name

class Student(Person):
    def __init__(self, name, gender, score):
        super(Student, self).__init__(name, gender)
        self.score = score
    def who(self):
        return 'I am a Student, my score is %s' % self.score

class Teacher(Person):
    def __init__(self, name, gender, course):
        super(Teacher, self).__init__(name, gender)
        self.course = course
    def who(self):
        return 'I am a Teacher,teching %s'%self.course
        
class SkillMixin(object):
    def __init__(self):
        pass
    
    def get_skill(self):
        print('I hava a skill')
        
class BasketballSkillMixin(SkillMixin):
    def __init__(self):
        super(BasketballSkillMixin,self).__init__()
        self.skill='basketball'
    
    def get_skill(self):
        print('I hava a %s skill'%self.skill)

class FootballSkillMixin(SkillMixin):
    def __init__(self):
        super(FootballSkillMixin,self).__init__()
        self.skill='football'
        
    def get_skill(self):
        print('I hava a %s skill'%self.skill)

class MyBasketStudent(BasketballSkillMixin,Student):
    def __init__(self,name,gender,score):
        Student.__init__(self,name,gender,score)
        BasketballSkillMixin.__init__(self)
        
class MyFootballTeacher(FootballSkillMixin,Teacher):
    def __init__(self,name,gender,course):
        Teacher.__init__(self,name,gender,course)
        FootballSkillMixin.__init__(self)
    
a=MyBasketStudent('xiaoming','man',98)
print(a.who())
print(a.get_skill())
# b=MyFootballTeacher('lixing','man','physical')
# print(b.who())
# print(b.get_skill())

http://img2.mukewang.com/63d3fb5f0001c2f803930202.jpg

疑问:为啥最后一行多打印了一个None?

提问者:慕码人2087912 2023-01-28 00:27

个回答

  • weixin_慕函数3435348
    2023-02-22 15:18:28

     print('I hava a %s skill'%self.skill)都改成return 'I hava a %s skill'%self.skill。

    或者print(a.get_skill())改成a.get_skill()直接运行,因为你定义的def get_skill(self):自带打印了。

  • 一打啤酒
    2023-01-30 23:12:20

    参考下这个呢https://www.zhihu.com/question/434250863,可能是这个原因