Chinaunix首页 | 论坛 | 博客
  • 博客访问: 386301
  • 博文数量: 112
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: Python/Ruby

2017-07-18 18:22:19

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

点击(此处)折叠或打开

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

  8. class BB(AA):
  9.     def __init__(self):
  10.         super(BB, self).__init__()
  11.         self.bb = 50
  12.     def aa(self,name):
  13.         print "%d is dd,%s is name,%d is bb" %(self.dd,name,self.bb)
  14.         print "{0} is dd,{1} is name,{2} is bb".format(self.dd,name,self.bb)
  15.         super(BB, self).aa(name)

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

阅读(540) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~