静态方法和类方法

 1 #coding=utf-8
 2 class MyClass(object):
 3     """docstring for ClassName"""
 4     def __init__(self, arg):
 5         super(object, self).__init__()
 6         self.arg = arg
 7         
 8     @staticmethod
 9     def staticFun(*arg):
10         '''
11         @staticmethod不需要表示自身对象的self和自身类的cls参数,
12         就跟使用函数一样。
13         '''
14         print(arg[0])
15 
16     @classmethod
17     def classFun(self,*arg):
18         '''
19         @classmethod也不需要self参数,
20         但第一个参数需要是表示自身类的cls参数。
21         '''
22         print(arg[0])
23 if __name__ == '__main__':
24     MyClass.staticFun('staticmethod')
25     MyClass.classFun('classmethod')

 

posted @ 2017-09-18 14:46  fosonR  阅读(163)  评论(0)    收藏  举报