欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中的getter和setter的方法使用詳解

 更新時(shí)間:2022年12月06日 14:26:47   作者:惡霸程序員388  
基本上,在面向?qū)ο缶幊陶Z(yǔ)言中,使用setter和getter方法的主要目的是為了確保數(shù)據(jù)的封裝,這篇文章主要介紹了Python的getter和setter的方法使用詳解,需要的朋友可以參考下

本文主要內(nèi)容:

  • 解釋setter和getter的使用方法解釋@property裝飾器的妙用
  • 在python中,setter和getter方法并不像其它編程語(yǔ)言中的那樣?;旧?,在面向?qū)ο缶幊陶Z(yǔ)言中,使用setter和getter方法的主要目的是為了確保數(shù)據(jù)的封裝。不像其它面向?qū)ο缶幊陶Z(yǔ)言,python中的私有變量并不是真正的隱藏字段。在python中,通常在以下情況會(huì)用到setter和getter方法:
  • 在獲取或者設(shè)置屬性值的時(shí)候使用setter和getter方法為其添加驗(yàn)證邏輯避免對(duì)類的某些字段直接訪問,比如類的私有變量不應(yīng)該被外部調(diào)用者直擊訪問或者修改

使用普通函數(shù)實(shí)現(xiàn)setter和getter方法
要實(shí)現(xiàn)setter和getter屬性,只是定義普通方法get()和set()并不能反產(chǎn)生任何特殊的行為,例如:

class Student(object):
    def __int(self, age=0):
        self._age = age
    
    # getter方法
    def get(self):
        return self._age
    
    # setter方法
    def set(self, value):
        self._age = value
        
xiaoming = Student()
# 使用setter方法設(shè)置age
xiaoming.set(20)
# 使用getter方法返回age
print(xiaoming.get())
print(xiaoming._age)

輸出:

20
20

在上面代碼中,set_age()和get_age()方法與普通方法并沒有什么兩樣,那么如何實(shí)現(xiàn)像getter和setter一樣的功能呢?這就要用到python中的特殊方法property()。

使用property()方法來(lái)實(shí)現(xiàn)setter和getter的行為
property()是python中的一個(gè)內(nèi)置方法,它創(chuàng)建并返回一個(gè)屬性對(duì)象。一個(gè)屬性對(duì)象有三個(gè)方法,getter()、setter()和delete()。property()內(nèi)置方法有四個(gè)參數(shù),property(fget,fset, fdel, doc)。fget是一個(gè)用于獲取屬性值的函數(shù),fset是一個(gè)用于設(shè)置屬性值的函數(shù),fdel是一個(gè)用于刪除屬性的函數(shù),doc用于為屬性創(chuàng)建文檔說(shuō)明。一個(gè)屬性兌現(xiàn)有三個(gè)方法,getter()、setter()和delete()分別制定fget、fset、fdel。

class Adult(object):
    def __int(self):
        self.__age = 0
    
    # 獲取屬性_age的值
    def get_age(self):
        print('getter() method called')
        return self.__age
    
    # 設(shè)置屬性_age的值
    def set_age(self, value):
        print('setter() method called')
        self.__age = value
    
    # 刪除屬性_age
    def del_age(self):
        del self.__age
        
    age = property(get_age, set_age, del_age)

laowang = Adult()
laowang.age = 60
print(laowang.age)

輸出:

setter() method called
getter() method called
60

在上面的代碼中,age就是一個(gè)屬性對(duì)象,它保證了對(duì)私有變量的安全訪問。

使用@property裝飾器來(lái)實(shí)現(xiàn)setter和getter的行為
除了上面使用property()的方法來(lái)實(shí)現(xiàn)getter、setter方法的行為,在python中還可以裝飾器@property來(lái)實(shí)現(xiàn)。@property是python的一個(gè)內(nèi)置裝飾器,使用裝飾器的目的是改變類的方法或者屬性,這樣調(diào)用者就無(wú)需在代碼中做任何改動(dòng)。

class Adult(object):    
    def __init__(self):
        self.__age = 0
    
    @property
    def age(self):
        print('getter() method called')
        return self.__age
        
    @age.setter
    def age(self, value):
        if value < 18:
            raise ValueError('Sorry, you are a child, games not allowed')
        print('setter() method called')
        self.__age = value
        
xiaoli = Adult()
xiaoli.age = 19
print(xiaoli.age)

輸出:

setter() method called
getter() method called
19

上面的代碼清晰地展示了如何用pythonic的方式使用@property裝飾器實(shí)現(xiàn)setter和getter屬性。同時(shí)實(shí)現(xiàn)了對(duì)屬性賦值時(shí)的有效性檢查。

到此這篇關(guān)于Python的getter和setter的方法使用詳解的文章就介紹到這了,更多相關(guān)Python的getter和setter的方法使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論