码迷,mamicode.com
首页 > 其他好文 > 详细

高封装的property方法

时间:2018-06-08 19:23:02      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:fun   TE   attribute   sed   known   getting   封装   class   fine   

class Person():
    def __init__(self):
        self.__age = 0

    def set_age(self, age):
        if age < 0 or age > 200:
            self.__age = 0
        else:
            self.__age = age

    def get_age(self):
        return self.__age

    a = property(get_age, set_age)

    # def __init__(self, fget=None, fset=None, fdel=None, doc=None):  # known special case of property.__init__
    """

    property(fget=None, fset=None, fdel=None, doc=None) -> property attribute

    fget is a function to be used for getting an attribute value, and likewise
    fset is a function for setting, and fdel a function for del‘ing, an
    attribute.  Typical use is to define a managed attribute x:

    """

p = Person()
p.a = 999
print(p.a)

  

高封装的property方法

标签:fun   TE   attribute   sed   known   getting   封装   class   fine   

原文地址:https://www.cnblogs.com/wf-skylark/p/9157156.html

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