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()

    提升了体验

  • 相关阅读:
    colock
    ToggleButton 和 Switch
    radioButon的使用
    kotlin中val和var的区别
    textEdit
    c++ 网络编程基础
    网格布局 GridLayout
    数组、指针和引用
    Hello Word
    Win7-U盘安装出现"We were unable to copy your files. "
  • 原文地址:https://www.cnblogs.com/LoganY/p/14330358.html
Copyright © 2011-2022 走看看