• Python之路


    本章内容

    1.  属性方法
    2. 类方法
    3. 静态方法

    属性方法

    属性方法就是通过使用装饰器 @property , 将一个方法变成一个静态属性 , 于是我们就可以通过访问属性 , 来或得一个方法的返回值

     1 from urllib.request import urlopen
     2 class Web_page:
     3     def __init__(self, url):
     4         self.url = url
     5         self.__content = None
     6     # 将content方法变成属性
     7     @property
     8     def content(self):
     9         # 返回私有属性
    10         return self.__content if self.__content else urlopen(self.url).read()
    11 con = Web_page('www.baidu.com')
    12 res = con.content
    13 print(res)

    在property中为我们实现了三种方法 , get , set , delete

     1 class Foo:
     2     # 获取属性
     3     @property
     4     def AAA(self):
     5         print("执行了get方法")
     6     # 设定属性值
     7     @AAA.setter
     8     def AAA(self, value):
     9         print("执行了set方法")
    10     # 删除属性
    11     @AAA.deleter
    12     def AAA(self):
    13         print("执行了delete方法")
    14 # 实例化
    15 f = Foo()
    16 # 获取属性
    17 f.AAA
    18 # 设置属性值,必须设置参数,即使不使用
    19 f.AAA = 'aaa'
    20 # 删除属性值
    21 del f.AAA
    22 '''
    23 执行结果:
    24 执行了get方法
    25 执行了set方法
    26 执行了delete方法
    27 '''

    换一种写法看看

     1 class Foo:
     2     def get_AAA(self):
     3         print('执行了get方法')
     4     def set_AAA(self,value):
     5         print('执行了set方法')
     6     def delete_AAA(self):
     7         print('执行了delete方法')
     8     # 实例化property类
     9     AAA = property(get_AAA, set_AAA, delete_AAA)
    10 # 实例化
    11 f = Foo()
    12 # 获取属性直接调用,执行了get_AAA
    13 f.AAA
    14 # 设置属性值,传入参数执行了set_AAA
    15 f.AAA = 'aaa'
    16 # 删除属性值,执行了delete_AAA
    17 del f.AAA
    18 '''
    19 执行结果:
    20 执行了get方法
    21 执行了set方法
    22 执行了delete方法
    23 '''

    实际应用

     1 class Goods:
     2     def __init__(self):
     3         # 原价
     4         self.original_price = 100
     5         # 折扣
     6         self.discount = 0.8
     7     @property
     8     def price(self):
     9         # 实际价格 = 原价 * 折扣
    10         new_price = self.original_price * self.discount
    11         return new_price
    12     @price.setter
    13     def price(self, value):
    14         self.original_price = value
    15     @price.deleter
    16     def price(self):
    17         del self.original_price
    18 goods = Goods()
    19 goods.price         # 获取打折后商品价格
    20 goods.price = 200   # 修改商品原价
    21 print(goods.price)
    22 del goods.price     # 删除商品原价

    类方法

    类方法是通过@classmethod装饰器 , 将普通方法变成类方法 , 类方法只能与类属性交互 , 不能访问实例变量 , 并且默认有一个cls参数传进来表示本类

     1 class Person:
     2     country = 'China'
     3     def __init__(self,name,age):
     4         self.name = name
     5         self.age = age
     6     @classmethod    
     7     def search(cls):
     8         # 在类方法中不能使用实例变量,会抛出AttributeError
     9         print("I come from {}".format(cls.country))
    10         # print("{} come from {}".format(self.name,cls.country))  报错
    11 p = Person('lyon','18')
    12 p.search()
    13 # 执行结果: I come from China

    PS:类方法中的默认参数可以改成self , 并不会改变结果 , 同样只能访问类变量 , 不能访问实例变量

    静态方法

    静态方法是通过@staticmethod装饰器将类中的方法变成一个静态方法

    静态方法就像静态属性一样 , 在类中可以通过 self. 的方式进行调用 , 但是静态是不能够访问实例变量或类变量的 , 也就是说静态方法中的self已经跟本类没有关系了 , 它与本类唯一的关联就是需要通过类名来进行调用

     1 class Person:
     2     country = 'China'
     3     def __init__(self,name,age):
     4         self.name = name
     5         self.age = age
     6     # 已经跟本类没有太大的关系了,所以类中的属性无法调用
     7     @staticmethod    
     8     def search():
     9         print("我是静态方法")
    10 p = Person('lyon','18')
    11 p.search()
    12 # 执行结果: 我是静态方法

    加上self , self只为一个普通参数而已

     1 class Person:
     2     country = 'China'
     3     def __init__(self,name,age):
     4         self.name = name
     5         self.age = age
     6     @staticmethod
     7     def search(self):
     8         print("{} come from {}".format(self.name,self.country))
     9 p = Person('lyon','18')
    10 # 将实例传入search方法中
    11 p.search(p)
    12 # 执行结果: lyon come from China
  • 相关阅读:
    mongodb 逻辑操作符
    mongodb 操作符
    js 对象拷贝的三种方法,深拷贝函数。
    html语义化
    移动端meta
    onmouseOver、onmouseOut 和 onmouseEnter、onmouseLeave的区别
    javascript阻止事件冒泡和浏览器的默认行为
    js中return的用法
    Ubuntu联网设置
    视窗宽高offset、client、scroll
  • 原文地址:https://www.cnblogs.com/lyonyang/p/7375650.html
Copyright © 2020-2023  润新知