Python-类方法、静态方法、实例方法
#coding=utf-8 class Clsf(): nm='Class name' def __init__(self,nm): self.nm=nm def normalMethod(self,nm): print('Instance name:'+self.nm) @classmethod def classMethod(cls,nm): print('Class name:'+cls.nm) @staticmethod def staticMethod(nm): print('Static name:'+nm) c=Clsf('name') print('Instance...') c.normalMethod('Hello') c.classMethod('Hello') c.staticMethod('Hello') print('Class...') #Clsf.normalMethod('Hello') Clsf.classMethod('Hello') Clsf.staticMethod('Hello')
Instance...
Instance name:name
Class name:Class name
Static name:Hello
Class...
Class name:Class name
Static name:Hello
实例可以调用实例方法、类方法、静态方法;
类可以调用类方法、静态方法,无法访问实例方法;
实例方法无法访问类属性;