类的super函数
super函数的作用
python子类继承父类的方法而使用的关键字.- 当子类继承父类后 ,就可以
使用父类的方法
- 当子类继承父类后 ,就可以
super函数的用法
| class Parent(object): | |
| def __init__(self): | |
| print('hello i am parent') | |
| class Child(Parent): | |
| def __init__(self): | |
| print('hello i am child') | |
| super(Child, self).__init__() #python3 括弧内的参数可以省略 | |
| # 当前类 类的实例 |
代码
| # coding:utf-8 | |
| class Parent(object): | |
| def __init__(self, p): | |
| print('hello i am parent %s ' % p) | |
| class Child(Parent): | |
| def __init__(self, c, p): | |
| # super(Child, self).__init__(p) | |
| super().__init__(p) | |
| # super(Child, self).__init__('parent') 也可以 | |
| print('hello i am child %s ' % c) | |
| if __name__ == '__main__': | |
| c = Child(c='Child', p='Parent') |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ZkeqのCoding日志!
评论






