網站首頁 編程語言 正文
1.什么是property
簡單地說就是一個類里面的方法一旦被@property裝飾,就可以像調用屬性一樣地去調用這個方法,它能夠簡化調用者獲取數據的流程,而且不用擔心將屬性暴露出來,有人對其進行賦值操作(避免使用者的不合理操作)。需要注意的兩點是
- 調用被裝飾方法的時候是不用加括號的
- 方法定義的時候有且只能有self一個參數
>>> class Goods():
def __init__(self,unit_price,weight):
self.unit_price = unit_price
self.weight = weight
@property
def price(self):
return self.unit_price * self.weight
>>> lemons = Goods(7,4)
>>>
>>> lemons.price
28
上面通過調用屬性的方式直接調用到 price 方法,property把復雜的處理過程封裝到了方法里面去,取值的時候調用相應的方法名即可。
2.property屬性定義的兩種方式
A、裝飾器方式
在類的方法上應用@property裝飾器,即上面那種方式。
B、類屬性方式
創建一個實例對象賦值給類屬性
>>> class Lemons():
def __init__(self,unit_price=7):
self.unit_price = unit_price
def get_unit_price(self):
return self.unit_price
def set_unit_price(self,new_unit_price):
self.unit_price = new_unit_price
def del_unit_price(self):
del self.unit_price
x = property(get_unit_price, set_unit_price, del_unit_price)
>>> fruit = Lemons()
>>>
>>> fruit.x #調用 fruit.x 觸發 get_unit_price
7
>>>
>>> fruit.x = 9 #調用 fruit.x = 9 觸發 set_unit_price
>>>
>>> fruit.x
9
>>>
>>> fruit.unit_price #調用 fruit.unit_price 觸發 get_unit_price
9
>>> del fruit.x #調用 del fruit.x 觸發 del_unit_price
>>>
>>> fruit.unit_price
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
l.unit_price
AttributeError: 'Lemons' object has no attribute 'unit_price'
property方法可以接收四個參數
- 第一個參數是獲得屬性的方法名,調用 對象.屬性時自動觸發
- 第二個參數是設置屬性的方法名, 給屬性賦值時自動觸發
- 第三個參數是刪除屬性的方法名,刪除屬性時自動觸發
- 第四個參數是字符串,是屬性的描述文檔,調用對象.屬性.doc時觸發
3.用property代替getter和setter方法
>>>class Watermelon():
def __init__(self,price):
self._price = price #私有屬性,外部無法修改和訪問
def get_price(self):
return self._price
def set_price(self,new_price):
if new_price > 0:
self._price = new_price
else:
raise 'error:價格必須大于零'
用property代替getter和setter
>>>class Watermelon():
def __init__(self,price):
self._price = price
@property #使用@property裝飾price方法
def price(self):
return self._price
@price.setter #使用@property裝飾方法,當對price賦值時,調用裝飾方法
def price(self,new_price):
if new_price > 0:
self._price = new_price
else:
raise 'error:價格必須大于零'
>>> watermelon = Watermelon(4)
>>>
>>> watermelon.price
4
>>>
>>> watermelon.price = 7
>>>
>>> watermelon.price
7
原文鏈接:https://blog.csdn.net/lbaihao/article/details/125351090
相關推薦
- 2022-05-23 vmware增加新硬盤無需重啟即可生效的命令腳本_VMware
- 2022-03-27 Android自定義View實現時鐘效果_Android
- 2023-01-15 圖鄰接矩陣可視化解析_python
- 2023-02-27 python定時任務sched庫用法簡單實例_python
- 2023-04-11 Go使用協程批量獲取數據加快接口返回速度_Golang
- 2022-08-03 C++類與對象深入之靜態成員與友元及內部類詳解_C 語言
- 2022-11-03 C++編譯期循環獲取變量類型詳情_C 語言
- 2023-11-17 Linux centos創建用戶以及賦予sudo權限
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支