-
一般方法使用 类生成的对象调用
-
静态方法用类直接调用
-
类方法用类直接调用类当参数传入方法
-
如下面例子:
-
###################################
-
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!")
-
-
-
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
python中@staticmethod 、@classmethod 与一般方法的区别,布布扣,bubuko.com
python中@staticmethod 、@classmethod 与一般方法的区别
原文:http://blog.csdn.net/fanyunlei/article/details/21954003