zoukankan      html  css  js  c++  java
  • property装饰器函数 @classmethod @staticmethod isinstance和issubclass

    property函数   让方法伪装成属性   还有一系列的装饰器函数

     1 class Goods:
     2     __discount = 0.8
     3     def __init__(self,price):
     4         self.__price = price
     5         self.name = 'apple'
     6 
     7     @property                 #伪装成属性
     8     def price(self):
     9         return self.__price * Goods.__discount
    10 
    11     @price.setter             #修改折扣
    12     def price(self,new):
    13         self.__price = new
    14 
    15     @price.deleter            #删除方法
    16     def price(self):
    17         del self.__price
    18 apple = Goods(10)
    19 print(apple.price)    
    20 print(apple.__dict__)
    21 del apple.price
    22 apple.price = 8
    23 print(apple.price)
    24 
    25 # print(apple.__dict__)
    26 # del apple.name
    27 # print(apple.__dict__)

    一个方法被伪装成属性之后,应该可以执行一个属性的增删改查操作

    那么增加和修改 就对应这被setter 装饰的方法:这个方法又必须传一个参数比如new,表示赋值的的是等号右边的值

    删除一个属性 对应着被 deleter 装饰的方法,这个方法并不能再执行的时候真的删除这个属性,而是你代码中执行什么 就有什么效果

    class A:
        def __init__(self):
            self.__f = open('aaa','w')
    
        @property
        def f(self):
            return self.__f
    
        @f.deleter
        def f(self):
            self.__f.close()
            del self.__f                       # 主要注意关闭文件句柄

    @classmethod

    class Goods:
        __discount = 0.8           # 静态属性
        def __init__(self,price):
            self.__price = price   #  对象属性
            self.name = 'apple'
        @property
        def price(self):
            print(self)
            return self.__price * Goods.__discount
        @classmethod
        def change_discount(cls,new):       # 类方法  修改了静态属性
            cls.__discount = new
    Goods.change_discount(0.7)
    print(Goods.__dict__) 
    apple = Goods(10)
    banana = Goods(20)
    apple.change_discount(0.7)
    print(apple.price)
    print(Goods)
    print(banana.price)

    #类方法的特点

       只使用类中的资源,并且这个资源可以直接用类名引用使用(不用对象名去调用),那这个方法应该被称为类方法

    @staticmethod

    class Student:
    
        @staticmethod
        def login(usr,pwd):
            print('IN LOGIN',usr,pwd)
    
    
    Student.login('user','pwd')             变成一个普通函数


    类:
    静态属性 类 所有的对象都统一拥有的属性
    类方法 类 如果这个方法涉及到操作静态属性、类方法、静态方法 cls 表示类
    静态方法 类 普通方法,不使用类中的命名空间也不使用对象的命名空间 : 一个普通的函数 没有默认参数
    方法 对象 self 表示对象
    property方法 对象 slef 表示对象

    isinstance 检测对象与类之间的关系

    issubclass 检测类与类之间的关系

    class A:
        conut = 1                   #静态属性
    
        def __init__(self,name):   #方法
            self.__name= name
        # @property                   #property方法
        # def name(self):
        #     return self.__name
        @classmethod
        def ppcount(cls,new):      #类方法
            cls.conut = new
        @staticmethod       #静态方法  变成普通的函数   我的话来说 就是直接退化变垃圾了
        def func(a):
            print('alex')
    p1 = A('ALEC')
    print(p1.__dict__)
    print(p1._A__name)
  • 相关阅读:
    Atitit.ati  str  字符串增强api
    Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#
    Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#
    Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
    Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
    Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)
    Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)
    swfupload 上传控件的配置
    SWFUpload下载地址
    js 转化类似这样的时间( /Date(1389060261000)/)问题
  • 原文地址:https://www.cnblogs.com/single82/p/9568570.html
Copyright © 2011-2022 走看看