类方法 @classmethod 静态方法@staticmethod

作者:晴天
链接:https://www.zhihu.com/question/20021164/answer/248086691
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

class Num: #普通方法:
                能用Num调用而不能用实例化对象调用 
                 def one(): print ('1') 
 #实例方法:能用实例化对象调用而不能用Num调用
                     def two(self): print ('2') 
 #静态方法:能用Num和实例化对象调用 
                     @staticmethod 
                     def three(): print ('3') 
 #类方法:第一个参数cls长什么样不重要,都是指Num类本身,调用时将Num类作为对象隐式地传入方法
                 @classmethod def go(cls): 
          cls.three() Num.one() 
 

猜你喜欢

转载自blog.csdn.net/shilaike2/article/details/79382778