为了账号安全,请及时绑定邮箱和手机立即绑定

例子中的代码怎么运行不起来鸭?

class Person(object):

    def __init__(self,name,gender):

        self.name=name

        self.gender=gender

    def whoAmI(self):

        return 'I am a Person, my name is %s'% self.name

    

class Student(Person):

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

        super(Person,self).__init__(name,gender)

        self.score=score

    def whoAmI(self):

        return 'I am a Person, my name is %s'% self.name

    

class Teacher(Person):

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

        super(Person,self).__init__(name,gender)

    def whoAmI(self):

        return 'I am a Person, my name is %s'% self.name


def who_am_i(x):

    print (x.whoAmI())

p=Person('Tim','Male')

s=Student('Bob','Male',88)

t=Teacher('Alice','Female','English')


who_am_i(p)

who_am_i(s)

who_am_i(t)

https://img1.sycdn.imooc.com//5c8fa1910001395912500779.jpg

正在回答

3 回答

调用父类的 super(本类类型,self当前实例对象).__init__(父类参数)

2 回复 有任何疑惑可以回复我~

明白自己的错误了,太大意

0 回复 有任何疑惑可以回复我~

class Person():

    def __init__(self,name,gender):


        self.name=name


        self.gender=gender


    def whoAmI(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 whoAmI(self):


        return 'I am a Person, my name is %s'% self.name


    


class Teacher(Person):


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


        super(Teacher,self).__init__(name,gender)


    def whoAmI(self):


        return 'I am a Person, my name is %s'% self.name




def who_am_i(x):


    print (x.whoAmI())


p=Person('Tim','Male')


s=Student('Bob','Male',88)


t=Teacher('Alice','Female','English')




who_am_i(p)


who_am_i(s)


who_am_i(t)


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
python进阶
  • 参与学习       255665    人
  • 解答问题       2949    个

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

进入课程

例子中的代码怎么运行不起来鸭?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信