Python 练习范例 43

模仿静态变量 (static)

#!/usr/bin/python
# -*- coding: UTF-8 -*-

class Num:
    nNum = 1
    def inc(self):
        self.nNum += 1
        print 'nNum = %d' % self.nNum

if __name__ == '__main__':
    nNum = 2
    inst = Num()
    for i in range(3):
        nNum += 1
        print 'The num = %d' % nNum
        inst.inc()

以上 Python 代码输出结果如下

The num = 3
nNum = 2
The num = 4
nNum = 3
The num = 5
nNum = 4

Python2 100 例

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.