Python类的静态方法解析
来源:互联网 发布:sort数组排序 编辑:程序博客网 时间:2024/06/15 07:17
只操作类的属性,不处理实例对象属性,无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
_________________________________________________________________
4 0
- Python类的静态方法解析
- python 的静态方法和静态类
- python的类方法和静态方法
- python的类方法和静态方法
- python的静态方法
- Python 静态类、静态方法
- Python类里的静态方法函数
- Python 类的静态字段和静态方法
- Python的类方法,静态方法,实例方法的区别
- Python的类方法,静态方法,实例方法的区别
- python 类的实例方法,静态方法,类方法辨析
- Python类的成员方法、类方法与静态方法
- Python 静态方法 类方法
- Python 静态方法 类方法
- Python 类方法 静态方法
- Python Class 的实例方法/类方法/静态方法
- python类方法,实例方法,静态方法的区别
- Python的类方法,对象方法,静态方法
- 关于 NSOperationQueue
- ettercap与driftnet来实现中间人攻击
- 使用python解析json文件
- document.getElementById().value在ie下不起作用,在火狐中可以
- 网络收敛比
- Python类的静态方法解析
- haha
- 从零开始学习CocosBuilder
- android中的MotionEvent 及其它事件处理
- ResourceCursorTreeAdapter
- 如何调试Makefile (2)
- C#写的照片上传代码--建立一个一般处理程序
- Palindrome Partitioning
- HDU 动态规划(46道题目)