登录
首页精彩阅读学习python类方法与对象方法
学习python类方法与对象方法
2017-08-19
收藏

学习python类方法与对象方法

这篇文章主要和大家一起学习python类方法与对象方法,从一个简单例子出发进行学习,本文实例针对python的类方法与对象方法进行学习研究,具体内容如下

classTest_Demo:
  TEST='test_value'
 
  def__init__(self,name,age):
    self.name=name
    self.age=age
  #static method
  @staticmethod
  deftest_static():
    returnTest_Demo.TEST
  #特性
  @property
  deftest_property(self):
    returnself.name+':'+str(self.age)
  #类方法
  @classmethod
  deftest_class(self):
    returnself.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不同的是:

类方法和静态方法可以访问类的静态变量(类变量,TEST),但都不能访问实例变量(即name,age)

如果访问了就会报错:

以上就是本文的全部内容吗,希望对大家的学习有所帮助。


数据分析咨询请扫描二维码

客服在线
立即咨询