只操作类的属性,不处理实例对象属性,无self方法函数。上边必须写@staticmethod,如果不写@staticmethod则方法被认为是实例方法,要求其第一个参数为self。静态方法可以通过类调用、实例调用触发。可被继承,子类或子类实例对象依然可以调用。
_________________________________________________________________
class aa:
x = 100
def __init__(self):
self.x = 10
self.y = 12
def hello(self, x):
return x + 1
@staticmethod
def pr():
print ‘aa class x ‘, aa.x
class bb(aa):
def __init__(self):
aa.__init__(self)
self.z = 14
a = aa()
print a.x, a.y
a.pr()
aa.pr()
b = bb()
print b.x, b.y
_________________________________________________________________
运行结果如下
10 12
aa class x 100
aa class x 100
10 12
aa class x 100
aa class x 100
_________________________________________________________________
_________________________________________________________________
class aa:
x = 100
def __init__(self):
self.x = 10
self.y = 12
def hello(self, x):
return x + 1
@staticmethod
def pr():
print ‘aa class x ‘, aa.x
class bb(aa):
def __init__(self):
aa.__init__(self)
self.z = 14
a = aa()
print a.x, a.y
a.pr()
aa.pr()
b = bb()
print b.x, b.y
_________________________________________________________________
运行结果如下
10 12
aa class x 100
aa class x 100
10 12
aa class x 100
aa class x 100
_________________________________________________________________