首页 > 其他 > 详细

高封装的property方法

时间:2018-06-08 19:10:55      阅读:204      评论:0      收藏:0      [点我收藏+]
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方法

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!