前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 类的super函数

Python 类的super函数

作者头像
Zkeq
发布2022-05-18 14:23:54
4550
发布2022-05-18 14:23:54
举报
文章被收录于专栏:Zkeq

类的super函数

super函数的作用
  • python子类继承父类方法而使用的关键字.
    • 子类继承父类后 ,就可以使用父类的方法
super函数的用法
代码语言:javascript
复制
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 括弧内的参数可以省略
        #      当前类  类的实例

代码
代码语言:javascript
复制
# 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')
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 类的super函数
    • super函数的作用
      • super函数的用法
        • 代码
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档