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

python Class:面向对象高级编程 @property

时间:2018-07-19 17:34:18      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:prope   integer   value   编程   bsp   transform   form   ISE   normal   

看不懂,先记录


一、

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


class Student(object):

    @property
    def score(self):
        return self._score

    @score.setter      #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 = Student()
s.score = 60
print s.score


运行结果:60


二、

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

class Student1(object):

    @property       #????
    def birth(self):
        return self._birth

    @birth.setter    #????
    def birth(self, value):
        self._birth = value

    @property       #????
    def age(self):
        return 2014 - self._birth


s = Student1()
s.birth = 60
print s.age


运行结果:60


python Class:面向对象高级编程 @property

标签:prope   integer   value   编程   bsp   transform   form   ISE   normal   

原文地址:http://blog.51cto.com/13502993/2147278

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