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

Python类里的静态方法函数

2014-03-17 09:26 330 查看
只操作类的属性,不处理实例对象属性,无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
_________________________________________________________________
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息