- 一般方法使用 类生成的对象调用
- 静态方法用类直接调用
- 类方法用类直接调用类当参数传入方法
- 如下面例子:
- ###################################
- 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