您的位置:首页 > 编程语言 > Python开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: