zoukankan      html  css  js  c++  java
  • python property属性

    • 定义时,在实例方法的基础上添加 @property 装饰器;并且仅有一个self参数
    • 调用时,无需括号

    三种@property装饰器

    #coding=utf-8
    # ############### 定义 ###############
    class Goods:
        """定义一个商品类
           第一种装饰器:@property
           第二种装饰器:@property方法名.setter
           第三种装饰器:@property方法名.deleter
        """
        @property
        def price(self):
            print('@property')
    
        @price.setter
        def price(self, value):
            print('@price.setter')
    
        @price.deleter
        def price(self):
            print('@price.deleter')
    
    # ############### 调用 ###############
    obj = Goods()
    obj.price          # 自动执行 @property 修饰的 price 方法,并获取方法的返回值
    obj.price = 123    # 自动执行 @price.setter 修饰的 price 方法,并将  123 赋值给方法的参数value
    del obj.price      # 自动执行 @price.deleter 修饰的 price 方法
    ``类属性``
    def get_price(self):
            print("get price...")
            return 100
    
        def set_price(self, value):
            """必须两个参数"""
            print("set price...")
            print(value)
    
        def del_price(self):
            print("del price")
    
        price = property(get_price, set_price, del_price, "相关描述...")
  • 相关阅读:
    格式与布局
    iframe
    tp
    头信息
    php 文件下载
    socket
    Flex 布局2
    Flex 布局
    下拉刷新
    选取一种类中含有某一属性值得元素的集合
  • 原文地址:https://www.cnblogs.com/lab-zj/p/12166454.html
Copyright © 2011-2022 走看看