python类方法与对象方法 学习python类方法与对象方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python类方法与对象方法 学习python类方法与对象方法

waited   2021-03-23 我要评论
想了解学习python类方法与对象方法的相关内容吗,waited在本文为您仔细讲解python类方法与对象方法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python类方法,python对象方法,下面大家一起来学习吧。

本文实例针对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)

 如果访问了就会报错:

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们