手机
当前位置:查字典教程网 >脚本专栏 >python >学习python类方法与对象方法
学习python类方法与对象方法
摘要:本文实例针对python的类方法与对象方法进行学习研究,具体内容如下classTest_Demo:TEST='test_value'def_...

本文实例针对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类方法与对象方法1

注:与php不同的是:

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

如果访问了就会报错:

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

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

【学习python类方法与对象方法】相关文章:

pyramid配置session的方法教程

Python的词法分析与语法分析

python二叉树遍历的实现方法

python中定义结构体的方法

netbeans7安装python插件的方法图解

python调用shell的方法

vc6编写python扩展的方法分享

python解析中国天气网的天气数据

python 文件与目录操作

Python时区设置方法与pytz查询时区教程

精品推荐
分类导航