Python类的实例属性详解
来源:互联网 发布:淘宝客服礼貌用语大全 编辑:程序博客网 时间:2024/05/16 16:10
实例属性
1.类被实例化后才会具有的属性
2.一般在_init_()方法中创建并初始化
3.直接使用即定义:self.<属性名>
4.引用方法:self.<属性名>
5.self用来表示类的实例的
例如:在类TestCss当中定义实例属性
class TestCss:
cssa = 'class-attribe'
def __init__(self):
self.a = 0
self.b = 10
def info(self):
print('a:',self.a,'b:',self.b)
if __name__ == '__main__':
tc = TestCss()
tc.info()
程序的运行结果为:
6.类外用实例名.属性名方式定义和引用
例如:
class TestCss:
cssa = 'class-attribe'
def __init__(self):
self.a = 0
self.b = 10
def info(self):
print('a:',self.a,'b:',self.b)
if __name__ == '__main__':
tc = TestCss()
tc.info()
if __name__ == '__main__':
tc = TestCss()
tc.info()
tc.color = 'red'
print(tc.color)
程序的运行结果为:
7.相同类的不同实例其实例属性是不相关的
例如:
lass TestCss:
cssa = 'class-attribe'
def __init__(self):
self.a = 0
self.b = 10
def info(self):
print('a:',self.a,'b:',self.b)
if __name__ == '__main__':
tc = TestCss()
tc.info()
tc = TestCss()
tca = TestCss()
tc.a = 100
tc.b = 200
tc.info()
tca.info()
程序的运行结果为:
8.一般不建议在_init_()方法之外中创建和初始化实例属性
9.一般不推荐类外定义和修改,修改可以单独定义方法。
原文链接:http://www.maiziedu.com/wiki/python/instance/
- Python类的实例属性详解
- python的类属性和实例属性
- python的类属性和实例属性
- python 的 类属性 与 实例属性
- python的类属性和实例属性
- python的类属性和实例属性
- python的类属性和实例属性
- Python Class的实例属性/类属性
- 类属性和实例属性详解(python)
- Python类属性,实例属性
- python 类,实例,模块的属性
- python类的类属性与实例属性
- python中类属性和实例属性的区别
- Python实例属性和类属性的区别
- Python类属性或实例属性的类型限制
- 关于Python类属性与实例属性的讨论
- 关于Python类属性与实例属性的讨论
- 关于Python类属性与实例属性的讨论
- WIN下xlrd的安装
- linux shell学习(中)
- HttpServletRequest源码介绍
- 二维数组作为参数传递问题
- liferay中工程类型说明
- Python类的实例属性详解
- 通过shape如何画一个圆?
- Oracle创建表空间、用户、赋予权限、修改密码
- 图的半径
- 压缩文件夹实例代码
- TextView设置超链接
- 【Docker】 导入对应的war包进行docker操作
- 重定向地址栏信息不改变
- HTML5自适应手机屏幕