目錄
Mixin 類(lèi)
Mixin 類(lèi)的實(shí)例
運(yùn)行流程
流程圖
《Python GUI Programming with Tkinter》作者的話(huà)
Mixin 類(lèi)
Mixin 類(lèi)只包含了一組特定的函數(shù)集合,而我們將會(huì)將其與其他類(lèi)進(jìn)行混合,從而生成一個(gè)適用于實(shí)際需要的新類(lèi)
Mixin 類(lèi)的實(shí)例
代碼改編自《Python GUI Programming with Tkinter》
class Displayer():
def display(self, message):
print('2:display:Displayer')
print(message)
class LoggerMixin():
def log(self, message, filename='logfile.txt'):
print('5:log:LoggerMixin')
with open(filename, 'a') as fh:
fh.write(message)
def display(self, message):
print('1:display:LoggerMixin')
super().display(message)
print('3:display:LoggerMixin')
self.log(message)
class MySubClass(LoggerMixin, Displayer):
def log(self, message):
print('4:log:MySubClass')
super().log(message, filename='subclasslog.txt')
subclass = MySubClass()
subclass.display("此字符串將顯示并記錄在subclasslog.txt中.")
E:\DataAnalysis\tools\python3\python.exe E:/DataAnalysis/tools/python3/project/money_analysis/text/object_test.py
1:display:LoggerMixin
2:display:Displayer
此字符串將顯示并記錄在subclasslog.txt中.
3:display:LoggerMixin
4:log:MySubClass
5:log:LoggerMixin
Process finished with exit code 0
運(yùn)行流程
'''In a multiple inheritance situation, super() does something a little more complex
than just standing in for the superclass. It look up the chain of inheritance using
something called the Method Resolution Order and determines the nearest class that defines
the method we’re calling.
在多繼承的環(huán)境下,super() 有相對(duì)來(lái)說(shuō)更加復(fù)雜的含義。它會(huì)查看你的繼承鏈,使用一種叫做 Methods
Resolution Order(方法解析順序) 的方式,來(lái)決定調(diào)用最近的繼承父類(lèi)的方法。'''
在我們調(diào)用 MySubClass.display() ,觸發(fā)行為如下:
1.MySubClass.display() 方法被解析為 LoggerMixin.display() 方法的調(diào)用
對(duì)于 MySubClass 類(lèi)來(lái)說(shuō),在繼承鏈上有兩個(gè)父類(lèi), LoggerMixin 最近,因此調(diào)用其方法。
2. LoggerMixin.display() 方法調(diào)用了 super().display()
在繼承鏈上,是逐個(gè)繼承的,后者是前者父類(lèi),查看運(yùn)行結(jié)果,調(diào)用 Displayer 類(lèi)的 display() 方法。
3.? LoggerMixin 類(lèi)還調(diào)用了 self.log() 方法。
盡管self在LoggerMixin 語(yǔ)境中,但是由MySubClass 繼承并調(diào)用,所以self指向MySubClass 實(shí)例,執(zhí)行了 MySubClass.log()
MySubClass.log() 執(zhí)行 super().log() 方法,根據(jù)繼承鏈尋找最近父類(lèi),執(zhí)行了 LoggerMixin.log()
流程圖
?
《Python GUI Programming with Tkinter》作者的話(huà)
''' If this seems confusing, just remember that self.method() will look for method() in
the current class first, then follow the list of inherited classes from left to right
until the method is found. The super().method() will do the same, except that it skips the
current class.
如果這看起來(lái)很混亂,請(qǐng)記住self.method()將首先在當(dāng)前類(lèi)中查找method(),然后從左到右跟隨繼承類(lèi)的
列表,直到找到該方法。super().method()將執(zhí)行相同的操作,只不過(guò)它跳過(guò)了當(dāng)前類(lèi)。 '''
為什么要叫做 Mixin 類(lèi)呢?
'''Note that LoggerMixin is not usable on its own: it only works when combined with a
class that has a display() method. This is why it’s a mixin class because it’s meant to be
mixed in to enhance other classes.
也就是說(shuō),我們的 LoggerMixin 類(lèi)是無(wú)法單獨(dú)使用的,它必須要和一個(gè)擁有 display() 函數(shù)定義的類(lèi)一起混合
使用。這也就是為什么它被稱(chēng)作是 Mixin 類(lèi)的原因,它總是需要與其他類(lèi)混合來(lái)加強(qiáng)其他類(lèi)。'''
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
