您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

学习python类方法与对象方法

5b51 2022/1/14 8:19:29 python 字数 2408 阅读 406 来源 www.jb51.cc/python

本文实例针对python的类方法与对象方法进行学习研究,具体内容如下 classTest_Demo:

概述

本文实例针对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不同的是:

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

 如果访问了就会报错:

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

总结

以上是编程之家为你收集整理的学习python类方法与对象方法全部内容,希望文章能够帮你解决学习python类方法与对象方法所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶