日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python学习(七)面向对象 ——封装

發(fā)布時(shí)間:2023/12/13 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python学习(七)面向对象 ——封装 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Python?類的封裝

?

  承接上一節(jié),學(xué)了Student類的定義及實(shí)例化,每個(gè)實(shí)例都擁有各自的name和score。現(xiàn)在若需要打印一個(gè)學(xué)生的成績(jī),可定義函數(shù) print_score()

  該函數(shù)為類外的函數(shù),如下:

1 class Student(object): 2 def __init__(self, name, score): 3 self.name = name 4 self.score = score 5 6 May = Student("May",90) # 須要提供兩個(gè)屬性 7 Peter = Student("Peter",85) 8 print(May.name, May.score) 9 print(Peter.name, Peter.score) 10 11 def print_score(Student): # 外部函數(shù)print_score(Student) 12 # print("%s's score is: %d" %(Student.name,Student.score)) # 普通 print 寫(xiě)法 13 print("{0}'s score is: {1}".format(Student.name,Student.score)) # 建議使用 Python 2.7 + .format優(yōu)化寫(xiě)法 14 print_score(May) 15 print_score(Peter)

?

  既然Student實(shí)例本身就擁有這些數(shù)據(jù),要訪問(wèn)這些數(shù)據(jù),就沒(méi)有必要從外面的函數(shù)去訪問(wèn),我們可以直接在Student類的內(nèi)部定義訪問(wèn)數(shù)據(jù)的函數(shù)。這樣,就把數(shù)據(jù)給“封裝”起來(lái)了。

  “封裝”就是將抽象得到的數(shù)據(jù)和行為(或功能)相結(jié)合,形成一個(gè)有機(jī)的整體(即類);封裝的目的是增強(qiáng)安全性和簡(jiǎn)化編程,使用者不必了解具體的實(shí)現(xiàn)細(xì)節(jié),而只是要通過(guò)外部接口,一特定的訪問(wèn)權(quán)限來(lái)使用類的成員。

  而這些封裝數(shù)據(jù)的函數(shù)是和Student類本身是關(guān)聯(lián)起來(lái)的,我們稱之為類的方法。那如何定義類的方法呢?

  就要用到對(duì)象 self?本身,參考上例,把 print_score()?函數(shù)寫(xiě)為類的方法(Python2.7之后的版本,推薦.format?輸出寫(xiě)法):

1 class Student(object): 2 def __init__(self, name, score): 3 self.name = name 4 self.score = score 5 6 def print_score(self): 7 print("{self.name}'s score is: {self.score}".format(self=self)) # Python 2.7 + .format優(yōu)化寫(xiě)法 8 9 May = Student("May",90) 10 Peter = Student("Peter",85)

?

  定義類的方法:除了第一個(gè)參數(shù)是self外,其他和普通函數(shù)一樣。

  實(shí)例調(diào)用方法:只需要在實(shí)例變量上直接調(diào)用,除了self不用傳遞,其他參數(shù)正常傳入;注意,若類的方法僅需要self,不需要其他,調(diào)用該方法時(shí),僅需 instance_name.function_name()

  這樣一來(lái),我們從外部看Student類,就只需要知道,創(chuàng)建實(shí)例需要給出name和score,而如何打印,都是在Student類的內(nèi)部定義的,這些數(shù)據(jù)和邏輯被“封裝”起來(lái)了,調(diào)用很容易,但卻不用知道內(nèi)部實(shí)現(xiàn)的細(xì)節(jié)。

  封裝的另一個(gè)好處是可以給Student類增加新的方法;這邊的方法也可以要求傳參,如新增定義compare 函數(shù),如下:

1 class Student(object): 2 def __init__(self, name, score): 3 self.name = name 4 self.score = score 5 6 def print_score(self): 7 print("{self.name}'s score is: {self.score}".format(self=self)) # Python 2.7 + .format優(yōu)化寫(xiě)法 8 9 def compare(self,s): 10 if self.score>s: 11 print("better than %d" %(s)) 12 elif self.score==s: 13 print("equal %d" %(s)) 14 else: 15 print("lower than %d" %(s)) 16 17 May = Student("May",90) 18 Peter = Student("Peter",85) 19 20 May.print_score() 21 Peter.print_score() 22 23 May.compare(100) 24 May.compare(90) 25 May.compare(89)

?

轉(zhuǎn)載于:https://www.cnblogs.com/feeland/p/4415645.html

總結(jié)

以上是生活随笔為你收集整理的Python学习(七)面向对象 ——封装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。