Python中类方法和静态方法
来源:互联网 发布:有趣的软件大全 编辑:程序博客网 时间:2024/05/02 15:51
1、类中的静态方法
要在类中使用静态方法,需在静态方法前面加上@staticmethod标记符,以表示下面的成员函数是静态函数。
使用静态方法的好处:其函数不需要self参数,可以通过类调用该方法,不需要定义该类实例(当然通过类实例调用也没有问题)。
2、类方法
类方法可以通过类或它的实例来调用,但该方法的第一个参数cls是定义该方法的类对象而不是实例对象。
3、示例
#coding:utf-8class A: def function(self): print "Call nomal method" @staticmethod #静态方法 def Function1(): print "Call static method" @classmethod #类方法 def Function2(cls): print "Call class method" print "cls.__name__ is ",cls.__name__if __name__ == '__main__': a = A() #类实例化 a.function() #调用类中普通方法 A.Function1() #调用类中静态方法 A.Function2() #调用类中类方法
结果:
Call nomal methodCall static methodCall class methodcls.__name__ is A
0 0
- Python中类方法和静态方法
- python中类方法和静态方法
- python中静态方法和类方法
- Python中静态方法和类方法
- python中类方法、实例方法和静态方法
- python中,类方法和静态方法区别。
- python中静态方法和类方法的区别
- Python 静态方法和静态类
- python 的静态方法和静态类
- python类:类方法和静态方法
- PYTHON中的静态方法和类方法
- python中的静态方法和类方法
- python类方法和静态方法
- [Python]静态方法和类方法
- PYTHON中的静态方法和类方法
- python类方法和静态方法
- python中的静态方法和类方法
- python类方法和静态方法
- IOS总结_IOS常用的方法集合、调用系统电话、设备区分、APP内永不锁屏
- Eclipse中跟踪调试Android源代码
- cocos2d-x V3.0 调用重力加速器 Acceleration
- dfjgfjhj
- SQL Server 查询处理中的各个阶段(SQL执行顺序)
- Python中类方法和静态方法
- win7 pci串行端口安装
- selenium+python下拉框的定位
- linux下用/proc/stat文件来计算cpu的利用率-c语言实现
- 的话就看电视剧看过味道
- Sublime Text安装插件管理 Package Control
- boost的multi_array的用法
- Linux tcpdump命令详解
- DHT网络