@property
class Student(object):
def get_score(self):
return self._score
def set_score(self, value):
if not isinstance(value, int):
raiseValueError('score must be an integer!')
if value <0 or value >100:
raise ValueError('score must between 0 ~ 100!')
self._score = value
s = Student()
s.set_score(60) 赋值变量
s.get_score() 取变量的值
要用两个方法操作
class Student(object):
@property
def score(self):
return self._score
@score.setter
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value
s.score = 60
s.score
只需要引用一个方法就可以实现取值和赋值
网友评论