super()内置方法的使用

时间:2021-03-15 10:36:46   收藏:0   阅读:0

1 引入

2 语法

super(type[, object-or-type])

3 实用

class Father:
    
    def __init__(self):
        self.name = ‘Zoe‘
        self.age = 18

    def active(self):
        print(‘%s今年%d岁‘ % (self.name, self.age))
        print(self)


class Son(Father):
    
    def __init__(self):
        # super() 首先找到 Son 的父类(就是类 Father),然后把类 Son 的对象转换为类 Father 的对象
        super().__init__()

    def active(self):
        # 调用父类中的方法
        super().active()
        print(self)

s = Son()
print(s.name, s.age)
s.active()

‘‘‘
Zoe 18
Zoe今年18岁
<__main__.Son object at 0x000001C413B87D88>
<__main__.Son object at 0x000001C413B87D88>
‘‘‘
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!