推荐使用的派生方法:super().__init__()

"""
推荐使用的派生方法:super().__init__()

--super(),严格继承属性查找顺序

--super(),会得到一个特殊的对象,该对象就是专门用来
访问父类中的属性的(严格按照继承的关系)

--super().__init__(),此处__init__(),
括号中不需要传值self。

--在Python2中,super()的完整用法是super(自己类名,self),
在Python2中需要写完整,而Python3中可以简写为super()。


"""

class OldboyPeople:
    school='oldboy'
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex

class OldboyStudent(OldboyPeople):

    def __init__(self,name,age,sex,stu_id):
        super().__init__(name,age,sex)
        self.stu_id=stu_id

    def choose_course(self):
        print('%s is choosing course'%self.name)
        # return 'true'
        #函数自带返回值none,如果把return 'true'这一行注释的话,
        # 那么打印的print(stu1.choose_course())这个结果就会是:tank is choosing course
        # 并且还会打印出有None。

stu1=OldboyStudent('tank',19,'male',1)
print(stu1.__dict__)
print(stu1.choose_course())

上述程序输出的打印结果如下所示:
{'name': 'tank', 'age': 19, 'sex': 'male', 'stu_id': 1}
tank is choosing course
None


猜你喜欢

转载自www.cnblogs.com/ludundun/p/11494933.html