欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

學(xué)習(xí)python類方法與對象方法

 更新時間:2016年03月15日 15:28:01   作者:waited  
這篇文章主要和大家一起學(xué)習(xí)python類方法與對象方法,從一個簡單例子出發(fā)進(jìn)行學(xué)習(xí),感興趣的小伙伴們可以參考一下

本文實(shí)例針對python的類方法與對象方法進(jìn)行學(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.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())

輸出結(jié)果:

注:與php不同的是:

 類方法和靜態(tài)方法可以訪問類的靜態(tài)變量(類變量,TEST),但都不能訪問實(shí)例變量(即name,age)

 如果訪問了就會報錯:

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

相關(guān)文章

最新評論