python使用@property

来源:互联网 发布:总决赛数据 编辑:程序博客网 时间:2024/06/17 20:58

直接使用修改属性不符合面向对象设计中的封装原则,但是使用get,set方法太过麻烦,python提供了@property方法来解决这个问题。

class Student(object):    @property    def score(self):        return self.__score    @score.setter    def score(self,value):        if not isinstance(value,int):            self.__score=value            s=Student()
 s.score=60
详细看这篇文章:http://python.jobbole.com/80955/



原创粉丝点击