zoukankan      html  css  js  c++  java
  • property装饰

    property装饰

    from math import pi
    class Circle:
        def __init__(self,r):
            self.r = r
        @property # 把一个方法伪装成一个属性,在调用这个方法的时候不需要加()就可以直接得到返回值
        def area(self):
            return pi * self.r ** 2
    r = Circle(5)
    print(r.r)
    print(r.area)
    
    import time
    class Person:
        def __init__(self,name ,birth):
            self.name = name
            self.birth = birth
        @property  # 装饰的这个方法 不能有参数
        def age(self):
            return time.localtime().tm_year - self.birth
    
    taibai = Person('taibai',2000)
    print(taibai.age)
    
    # property的第二个应用场景 : 和私有的属性合作的  -这样可以做到只能查看无法修改
    class User:
        def __init__(self,usr,pwd):
            self.usr = usr
            self.__pwd = pwd
        @property
        def pwd(self):
            return self.__pwd
    alex = User('alex','sbsbsb')
    # print(alex.pwd)
    # alex.pwd = 111 #报错
    --------------------------------
    # class Goods:
    #     discount = 0.8
    #     def __init__(self,name,origin_price):
    #         self.name = name
    #         self.__price = origin_price
    #     @property
    #     def price(self):
    #         return self.__price * self.discount
    #
    # apple = Goods('apple',5)
    # print(apple.price)
    
    property进阶
    class Goods:
        discount = 0.8
        def __init__(self,name,origin_price):
            self.name = name
            self.__price = origin_price
        @property
        def price(self):
            return self.__price * self.discount
    
        @price.setter
        def price(self,new_value):
            if isinstance(new_value,int):
                self.__price = new_value
    
    apple = Goods('apple',5)
    print(apple.price)   # 调用的是被@property装饰的price
    apple.price = 10     # 调用的是被setter装饰的price
    print(apple.price)
    
    class Goods:
        discount = 0.8
        def __init__(self,name,origin_price):
            self.name = name
            self.__price = origin_price
        @property
        def price(self):
            return self.__price * self.discount
    
        @price.setter
        def price(self,new_value):
            if isinstance(new_value,int):
                self.__price = new_value
    
        @price.deleter
        def price(self):
            del self.__price
    apple = Goods('apple',5)
    print(apple.price)
    apple.price = 'ashkaksk'
    del apple.price   # 并不能真的删除什么,只是调用对应的被@price.deleter装饰的方法而已
    print(apple.price)
    
    # property进阶
    class Goods:
        discount = 0.8
        def __init__(self,name ,origin_price):
            self.name = name
            self.__price = origin_price
    
        @property
        def price(self):
            return self.__price * self.discount
    
        @price.setter
        def price(self,new_value):
            if isinstance(new_value,int):
                self.__price = new_value
    
        @price.deleter
        def price(self):
            del self.__price
    
    
    apple = Goods('apple',5)
    print(apple.price)
    apple.price = 9
    print(apple.price)
    del apple.price   # 并不能真的删除什么,只是调用对应的被@price.deleter装饰的方法而已
    
  • 相关阅读:
    Android Xmpp协议讲解
    IOS 教程以及基础知识
    android的快速开发框架集合
    Android项目快速开发框架探索(Mysql + OrmLite + Hessian + Sqlite)
    afinal logoAndroid的快速开发框架 afinal
    Android 快速开发框架:ThinkAndroid
    2020.12.19,函数式接口,函数式编程,常用函数式接口,Stream流
    2020.12.18 网络编程基础,网络编程三要素,TCP通信,Socket类,ServerSocket
    2020.12.16,Properties,Buffer,InputStreamReader
    2020.12.15 IO流,字节流,字符流,流异常处理
  • 原文地址:https://www.cnblogs.com/wyh0717/p/13198738.html
Copyright © 2011-2022 走看看