Python 静态方法 类方法
来源:互联网 发布:关系数据库的基本概念 编辑:程序博客网 时间:2024/06/07 16:10
Python 模拟静态方法:
#staticmethod.py
class Spam:
numInstances=0
#----------------------------------------------------------------------
def __init__(self):
""""""
Spam.numInstances=Spam.numInstances+1
#----------------------------------------------------------------------
def printNumInstances(self):
""""""
print 'number of instances created: ',Spam.numInstances
a,b,c=Spam(),Spam(),Spam()
a.printNumInstances()
b.printNumInstances()
c.printNumInstances()
Spam().printNumInstances()
执行结果:
number of instances created: 3
number of instances created: 3
number of instances created: 3
number of instances created: 4
使用Python的函数转换成静态方法:staticmethod()--静态方法 classmethod()类方法
例子:
class Multi:
def imeth(self,x):
print self,x
def smeth(x):
print x
def cmeth(cls,x):
print cls,x
smeth=staticmethod(smeth)
cmeth=classmethod(cmeth)
obj=Multi()
obj.imeth(1)
Multi.imeth(obj,2)
执行结果:
<__main__.Multi instance at 0x01ED26C0> 1
<__main__.Multi instance at 0x01ED26C0> 2
- Python 静态类、静态方法
- Python 静态方法 类方法
- Python 静态方法 类方法
- Python 类方法 静态方法
- python类方法、实例方法、静态方法
- Python 静态方法 类方法 实例方法
- python实例方法,静态方法,类方法
- python --类方法、对象方法、静态方法
- Python:类方法、静态方法、实例方法
- Python普通方法、静态方法、类方法
- python --类方法、对象方法、静态方法
- Python普通方法、静态方法、类方法
- python实例方法、静态方法、类方法
- python普通方法、类方法、静态方法
- python类方法 实例方法 静态方法
- python实例方法、类方法、静态方法
- python--类方法、对象方法、静态方法
- python--类方法、对象方法、静态方法
- WordPress函数:query_posts(查询文章)
- iOS应用开发:什么是ARC?
- SDUTOJ 3058 路线冲突问题 ——bfs记录路径
- iOS开发教程:Storyboard全解析-第一部分
- 手机客户端版csdn搜索
- Python 静态方法 类方法
- ASCII码表
- 开始了
- 一些有用的实例
- 策略模式(strategy)行为型模式c#简单例子
- html中inline与block的细节对比
- 开放平台之我见
- 面试一些有用的
- 大数据浅析