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
_________________________________________________________________
上边必须写@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
_________________________________________________________________
相关文章推荐
- Python动态类型的学习---引用的理解
- 垃圾邮件过滤器 python简单实现
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- Python使用RethinkDB总结
- Python,Flex 2和Aptana[js开放工具]
- 国外开发者谈为何放弃PHP而改用Python
- 利用webqq协议使用python登录qq发消息源码参考
- python 判断自定义对象类型
- 让python的Cookie.py模块支持冒号做key的方法
- Python 面向对象 成员的访问约束
- 新手该如何学python怎么学好python?