python中@staticmethod 、@classmethod 与一般方法的区别
来源:互联网 发布:淘宝代销物流怎么设置 编辑:程序博客网 时间:2024/06/11 13:57
- 一般方法使用 类生成的对象调用
- 静态方法用类直接调用
- 类方法用类直接调用类当参数传入方法
- 如下面例子:
- ###################################
- class Person:
- def __init__(self):
- print "init"
- @staticmethod
- def sayHello(hello):
- if not hello:
- hello='hello'
- print "i will sya %s" %hello
- @classmethod
- def introduce(clazz,hello):
- clazz.sayHello(hello)
- print "from introduce method"
- def hello(self,hello):
- self.sayHello(hello)
- print "from hello method"
- def main():
- Person.sayHello("haha")
- Person.introduce("hello world!")
- #Person.hello("self.hello") #TypeError: unbound method hello() must be called with Person instance as first argument (got str instance instead)
- print "*" * 20
- p = Person()
- p.sayHello("haha")
- p.introduce("hello world!")
- p.hello("self.hello")
- if __name__=='__main__':
- main()
output:
- i will sya haha
- i will sya hello world!
- from introduce method
- ********************
- init
- i will sya haha
- i will sya hello world!
- from introduce method
- i will sya self.hello
- from hello method
1 0
- python中@staticmethod 、@classmethod 与一般方法的区别
- Python中classmethod与staticmethod区别
- Python中classmethod与staticmethod区别
- python 中 @classmethod 、staticmethod 和 普通方法 的区别
- python中 classmethod、staticmethod和普通方法的区别
- python中@classmethod与@staticmethod的区别和用法
- python中staticmethod和classmethod的区别
- python中classmethod和staticmethod的区别
- python中staticmethod和classmethod的区别
- python中@classmethod @staticmethod区别
- Python中staticmethod方法和classmethod方法区别
- python的静态方法以及@staticmethod或@classmethod的区别
- 飘逸的python - @staticmethod和@classmethod的作用与区别
- python - @staticmethod和@classmethod的作用与区别
- python @staticmethod和@classmethod的作用与区别
- Python: classmethod & staticmethod 区别
- Python: classmethod & staticmethod 区别
- Python: classmethod & staticmethod 区别
- OrmLite数据库开源组件学习(1)
- Linux---rrdtool编译时的报错
- Linux系统调用--getpid/getppid函数详解
- Android Wifi work station Framework and Architecture
- android使用Intent传递数据 2 种方式(Intent和Bundle)
- python中@staticmethod 、@classmethod 与一般方法的区别
- 跟我一起写 Makefile(一)
- android 多屏幕支持
- Android 的定向广播 局部广播
- UVa 489 - Hangman Judge
- delete和delete[]的区别
- javaweb之自定义标签——简单标签库
- Hadoop权限管理(转)
- C++ 模板设计 policy和traits