本文實例針對python的類方法與對象方法進行學(xué)習(xí)研究,具體內(nèi)容如下
class Test_Demo: TEST = 'test_value' def __init__(self,name,age): self.name = name self.age = age #static method @staticmethod def test_static(): return Test_Demo.TEST #特性 @property def test_property(self): return self.name+':'+str(self.age) #類方法 @classmethod def test_class(self): return self.TESTif __name__ == '__main__': test_demo = Test_Demo('zj',23) #print(test_demo.name) print(Test_Demo.test_static()) print(test_demo.test_property) print(test_demo.test_class()) 輸出結(jié)果:

注:與php不同的是:
類方法和靜態(tài)方法可以訪問類的靜態(tài)變量(類變量,TEST),但都不能訪問實例變量(即name,age)
如果訪問了就會報錯:

以上就是本文的全部內(nèi)容嗎,希望對大家的學(xué)習(xí)有所幫助。



















