ITPub博客

首页 > 应用开发 > > super()函数

super()函数

作者:deepocean601 时间:2019-09-02 14:33:45 0 删除 编辑

类的继承过程中,子类经常会重写一些父类的方法,导致父类的方法不能用。如果要想调用父类的方法可以使用super(子类,self).方法。

#!/usr/bin/env python
# coding=utf-8
class AA(object):
    def __init__(self):
        self.dd = 30
    def aa(self,name):
        print "{0} is {1}".format(name,self.dd)

class BB(AA):
    def __init__(self):
        super(BB, self).__init__()
        self.bb = 50
    def aa(self,name):
        print "%d is dd,%s is name,%d is bb" %(self.dd,name,self.bb)
        print "{0} is dd,{1} is name,{2} is bb".format(self.dd,name,self.bb)
        super(BB, self).aa(name)

if __name__ == "__main__":
    cc = BB()
    cc.aa("hehe")


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2557/viewspace-3169001/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    102
  • 访问量
    475