可如下示例:
#!/usr/bin/python #coding:utf-8 class Person(object): 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() 运行结果如下:
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