这篇文章主要和大家一起学习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())输出结果:

立即学习“Python免费学习笔记(深入)”;
注:与php不同的是:
类方法和静态方法可以访问类的静态变量(类变量,TEST),但都不能访问实例变量(即name,age)
如果访问了就会报错:

以上就是本文的全部内容吗,希望对大家的学习有所帮助。
更多python类方法与对象方法介绍相关文章请关注PHP中文网!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号