python继承中的变量
2016-06-15 00:00
555 查看
class Parent(object):
DEFAULT_VALUE = 'parent'
def func1(self):
print 'parent : ' + self.DEFAULT_VALUE
class Child(Parent):
DEFAULT_VALUE= 'child'
def update_value(self):
print 'before : ' + self.DEFAULT_VALUE
self.DEFAULT_VALUE = 'updated_child_value'
print 'after : ' + self.DEFAULT_VALUE
def update_value_reset(self):
print 'reset before : ' + self.DEFAULT_VALUE
self.DEFAULT_VALUE = 'child'
print 'reset after : ' + self.DEFAULT_VALUE
if __name__ == '__main__' :
c = Child()
c.update_value()
c.func1()
print 'child value : ' + c.DEFAULT_VALUE
d = Child() # 新对象
d.func1()
Output:
before : child
after : updated_child_value
parent : updated_child_value
child value : updated_child_value
parent : child
if __name__ == '__main__' :
c = Child()
c.update_value()
c.func1()
c.update_value_reset()
c.func1()
Output:
before : child
after : updated_child_value
parent : updated_child_value
reset before : updated_child_value
reset after : child
parent : child
DEFAULT_VALUE = 'parent'
def func1(self):
print 'parent : ' + self.DEFAULT_VALUE
class Child(Parent):
DEFAULT_VALUE= 'child'
def update_value(self):
print 'before : ' + self.DEFAULT_VALUE
self.DEFAULT_VALUE = 'updated_child_value'
print 'after : ' + self.DEFAULT_VALUE
def update_value_reset(self):
print 'reset before : ' + self.DEFAULT_VALUE
self.DEFAULT_VALUE = 'child'
print 'reset after : ' + self.DEFAULT_VALUE
if __name__ == '__main__' :
c = Child()
c.update_value()
c.func1()
print 'child value : ' + c.DEFAULT_VALUE
d = Child() # 新对象
d.func1()
Output:
before : child
after : updated_child_value
parent : updated_child_value
child value : updated_child_value
parent : child
if __name__ == '__main__' :
c = Child()
c.update_value()
c.func1()
c.update_value_reset()
c.func1()
Output:
before : child
after : updated_child_value
parent : updated_child_value
reset before : updated_child_value
reset after : child
parent : child
相关文章推荐
- python使用Visual Studio 2015作为IDE开发django项目的环境部署
- 设计模式之策略模式
- python web cgi
- python 类变量和对象变量的区别联系-----类的基础(二)
- python类的继承--------类的基础(四)
- Python中open函数注意点
- 《笨办法学Python》完结报告
- Python 打印执行错误行数
- Python:判断一个字典里面key是否存在
- Python序列——序列操作
- [python]生成器
- python中type和instance
- 位置函数之坑
- 百度某站点python模板远程代码执行(在不支持多行和exec的情形执行系统命令) 20160614
- PYTHON 获取csdn的博客文章标题和描述
- Python 获取百度的预览图片
- python学习
- 让IIS7.5 执行Python脚本的配置方法
- Python 的Except 异常处理
- Python高级特性