python的类方法和静态方法
2014-12-13 00:00
696 查看
本文实例讲述了python的类方法和静态方法。分享给大家供大家参考。具体分析如下:
python没有和C++中static关键字,它的静态方法是怎样的呢?还有其它语言中少有的类方法又是神马?
python中实现静态方法和类方法都是依赖于python的修饰器来实现的。
大家注意到普通的对象方法、类方法和静态方法的去别了吗?
对象方法有self参数,类方法有cls参数,静态方法是不需要这些附加参数的。
在C++中是没有类方法着个概念的的
结果是:
Call static method foo1()
Call class method foo2()
cls.__name__ is A
希望本文所述对大家的Python程序设计有所帮助。
python没有和C++中static关键字,它的静态方法是怎样的呢?还有其它语言中少有的类方法又是神马?
python中实现静态方法和类方法都是依赖于python的修饰器来实现的。
class MyClass: def method(self): print("method") @staticmethod def staticMethod(): print("static method") @classmethod def classMethod(cls): print("class method")
大家注意到普通的对象方法、类方法和静态方法的去别了吗?
对象方法有self参数,类方法有cls参数,静态方法是不需要这些附加参数的。
在C++中是没有类方法着个概念的的
class A(object): "This ia A Class" @staticmethod def Foo1(): print("Call static method foo1()\n") @classmethod def Foo2(cls): print("Call class method foo2()") print("cls.__name__ is ",cls.__name__) A.Foo1(); A.Foo2();
结果是:
Call static method foo1()
Call class method foo2()
cls.__name__ is A
希望本文所述对大家的Python程序设计有所帮助。
相关文章推荐
- Python中静态方法的实现
- python基础3--面向对象--类变量、实例变量、类方法、静态方法、实例方法
- Python 静态方法和静态类
- Python中的静态方法-pybatis的准备阶段(五)
- [Python]静态方法和类方法
- Python中静态方法的实现
- Python的静态方法和类成员方法
- python里关于类方法和静态方法的区别
- python类方法和静态方法
- python中的静态成员方法 和 类成员方法
- [Dynamic Language] Python 静态方法、类方法、属性
- [总结]python 静态方法与类方法
- python类方法和静态方法
- 在python种实现类的静态方法
- PYTHON中的静态方法和类方法
- python的静态方法
- python的类方法和类的静态方法
- python 静态方法和类方法
- Python 静态方法与类方法