Python 的类中,主要会使用的两种变量:类变量与成员变量。类变量是类所有实例化对象共有的,而成员变量是每个实例化对象自身特有的。
# -*- coding: utf-8 -*-
class ClassTest():
static_a = 'AppBlog.CN'
def __init__(self):
self.a = 'Hello World'
pass
def method(self):
print '成员方法: method'
@staticmethod
def static_method():
print '静态方法: static_method'
if __name__=='__main__':
print '---------- 通过类调用 ----------'
print '类变量: ' + ClassTest.static_a
ClassTest.static_method()
print '---------- 通过对象调用 ----------'
test = ClassTest()
print '类变量: ' + test.static_a
print '成员变量: ' + test.a
test.static_method()
test.method()
运行结果:
---------- 通过类调用 ----------
类变量: AppBlog.CN
静态方法: static_method
---------- 通过对象调用 ----------
类变量: AppBlog.CN
成员变量: Hello World
静态方法: static_method
成员方法: method




