zoukankan      html  css  js  c++  java
  • python类中的property方法

    property是一个装饰器,可以在类中装饰在函数上,主要作用是对类中的添加,删除,更改做隐藏操作

    原先操控添加,删除,更改一个被封装的属性

    class EGG:
        __count=0
        def get_count(self):
            return self.__count
        
        def set_count(self,num):
            self.__count=num
        
        def del_count(self):
            del self.__count
    a=EGG()
    a.get_count()
    a.set_count()
    a.del_count()

    但是对操纵者来说不希望这样的体验更改后

    class EGG:
        __count=0
        def get_count(self):
            return self.__count
    
        def set_count(self,num):
            self.__count=num
    
        def del_count(self):
            del self.__count
        使用property(get,set,del)
        count=property(get_count,set_count,del_count)

    class EGG:
    __count=0
    @property
    def count(self):
    return self.__count
    @count.setter
    def count(self,num):
    self.__count=num
    @count.deleter
    def count(self):
    del self.__count



    a=EGG()
    a.count()
    a.count(1)
    a.count()

    提升了体验

  • 相关阅读:
    SpringMVC框架(2)
    SpringMVC框架(1)
    (数据结构与算法)跳表实现
    第2次作业
    第1次作业
    第一次作业
    第一周作业.
    第一周作业
    第0次作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/LoganY/p/14330358.html
Copyright © 2011-2022 走看看