码迷,mamicode.com
首页 > 编程语言 > 详细

python类创建对象的公有属性和私有属性

时间:2015-01-04 19:32:23      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:python 学习

>>> bart = Student(‘Bart Simpson‘, 59)
>>> lisa = Student(‘Lisa Simpson‘, 87)
>>> bart.age = 8
>>> bart.age
8
>>> lisa.age
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: ‘Student‘ object has no attribute ‘age‘

在类里面定义的变量是共有的,创建的每个对象都会继承
在类外面定义的变量是私有的,只有自己这个类拥有,别的没有


python类创建对象的公有属性和私有属性

标签:python 学习

原文地址:http://wuxiaoyu.blog.51cto.com/9202912/1599030

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!