新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:Python中裝飾屬性的方法
1、使用 get、set 方法來封裝對(duì)一個(gè)屬性的訪問在很多面向?qū)ο缶幊痰恼Z言中都很常見。

class Student(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_score(self):
return self.__score
def set_score(self, score):
self.__score = score
s = Student('zhangsan', 90)
s.set_score(100)
print(s.get_score())
# 輸出1002、Python里提供了@property裝飾器,可以把方法“裝飾”成屬性調(diào)用。
class Student(object):
def __init__(self, name, score):
self.name = name
self.__score = score
@property
def score(self):
return self.__score
@score.setter
def score(self, score):
self.__score = score
s = Student('zhangsan', 90)
s.score = 100
print(s.score)
# 輸出100以上就是Python中裝飾屬性的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)推薦:python教學(xué)
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)頁題目:創(chuàng)新互聯(lián)Python教程:Python中裝飾屬性的方法
分享路徑:http://fisionsoft.com.cn/article/coiihip.html


咨詢
建站咨詢
