學習python類方法與對象方法
更新時間:2016年03月15日 15:28:01 作者:waited
這篇文章主要和大家一起學習python類方法與對象方法,從一個簡單例子出發(fā)進行學習,感興趣的小伙伴們可以參考一下
本文實例針對python的類方法與對象方法進行學習研究,具體內容如下
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.TEST
if __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())
輸出結果:

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

以上就是本文的全部內容嗎,希望對大家的學習有所幫助。
相關文章
TensorFlow實現模型斷點訓練,checkpoint模型載入方式
這篇文章主要介紹了TensorFlow實現模型斷點訓練,checkpoint模型載入方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python基于socketserver實現并發(fā),驗證客戶端的合法性
TCP協議的socket一次只能和一個客戶端通信, 而socketsever可以時間和多個客戶端通信。本文將講解socketserver的具體使用2021-05-05
PyTorch中torch.tensor()和torch.to_tensor()的區(qū)別
在Pytorch中Tensor和tensor都用于生成新的張量,但二者并不相同,下面這篇文章主要給大家介紹了關于PyTorch中torch.tensor()和torch.to_tensor()區(qū)別的相關資料,需要的朋友可以參考下2023-01-01

