python 类方法、类实例方法、静态方法
2013-03-06 17:46
239 查看
使用方法:
Python代码
调用方法:
Python代码
区别:
类方法和静态方法都可以被类和类实例调用,类实例方法仅可以被类实例调用
类方法的隐含调用参数是类,而类实例方法的隐含调用参数是类的实例,静态方法没有隐含调用参数
Python代码
class A(object): def foo(self,x): #类实例方法 print "executing foo(%s,%s)"%(self,x) @classmethod def class_foo(cls,x): #类方法 print "executing class_foo(%s,%s)"%(cls,x) @staticmethod def static_foo(x): #静态方法 print "executing static_foo(%s)"%x
调用方法:
Python代码
a = A() a.foo(1) //print : executing foo(<__main__.A object at 0xb77d67ec>,1) a.class_foo(1) //executing class_foo(<class '__main__.A'>,1) A.class_foo(1) //executing class_foo(<class '__main__.A'>,1) a.static_foo(1) //executing static_foo(1) A.static_foo(1) //executing static_foo(1)
区别:
类方法和静态方法都可以被类和类实例调用,类实例方法仅可以被类实例调用
类方法的隐含调用参数是类,而类实例方法的隐含调用参数是类的实例,静态方法没有隐含调用参数
相关文章推荐
- 【python】详解类class的方法:实例方法、类方法、静态方法(三)
- Python类的实例方法、静态方法和类方法
- python 详解类class的方法:实例方法、类方法、静态方法(三)
- Python中的类属性、实例属性与类方法、静态方法
- Python:类属性,实例属性,私有属性与静态方法,类方法,实例方法 属性分为实例属性与类属性 方法分为普通方法,类方法,静态方法 一:属性: 尽量把需要用户传入的属性作为实例属性,而
- Python_实例方法、类方法、静态方法
- 类的实例方法,类方法,静态方法(python)
- python类的实例方法\静态方法\类方法区别解析(附代码)
- Python中的类属性和实例属性以及静态方法和类方法
- python 类属性 实例属性 类方法 实例方法 静态方法(转载)
- Python中的类属性和实例属性以及静态方法和类方法
- python中的实例方法、静态方法、类方法、类变量和实例变量浅析
- python中类方法、类实例方法、静态方法的使用与区别
- python中面向对象的类,方法,实例
- python 定义实例方法
- python 类的实例方法,静态方法,类方法辨析和实例讲解
- [Python进阶-3]面向对象,类,实例,属性,方法,类属性,类方法,内部属性
- python 类和实例绑定属性和方法的总结
- Python的高级特性8:你真的了解类,对象,实例,方法吗
- Python:类属性,实例属性,私有属性与静态方法,类方法,实例方法