第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在python中,如何讓兩個(gè)類共享一個(gè)方法的實(shí)現(xiàn),該方法調(diào)用另一個(gè)方法以及每個(gè)類的特定實(shí)現(xiàn)

在python中,如何讓兩個(gè)類共享一個(gè)方法的實(shí)現(xiàn),該方法調(diào)用另一個(gè)方法以及每個(gè)類的特定實(shí)現(xiàn)

喵喵時(shí)光機(jī) 2023-09-05 17:26:49
假設(shè)我有一個(gè)父類Parent,其抽象方法load在其子類中實(shí)現(xiàn)?,F(xiàn)在,我有兩個(gè)子類childA和childB,都繼承自Parent。loadfor的實(shí)現(xiàn)childA和childB完全相同。但是 的此實(shí)現(xiàn)load調(diào)用另一個(gè)方法 ,get_paths該方法特定于childA和childB。父母是這樣的:from abc import ABC, abstractmethodclass Parent(ABC):    def __init__(self, *params):        # set up the params    @abstractmethod    def load(self, *params):        # load files, according to each children    def process(self, *params):        # process the loaded files然后是孩子們:class childA(Parent):    def __init__(self, *paramsP, *paramsA):        super().__init__(*paramsP)        # Set up the specific paramsA    def get_paths(self, *params):        # specific implementation to childA    def load(self, *params):        # Calls the self.get_paths method, identical to childB.loadclass childB(Parent):    def __init__(self, *paramsP, *paramsB):        super().__init__(*paramsP)        # Set up the specific paramsB    def get_paths(self, *params):        # specific implementation to childB    def load(self, *params):        # Calls the self.get_paths method, identical to childA.load如何重用和load中的代碼,但實(shí)現(xiàn)特定于每個(gè)類?我嘗試了兩種方法:childAchildBget_paths(1) 繼承childB自childA,并覆蓋 的實(shí)現(xiàn)get_paths。在這種方法中,我有:class childB(childA):    def __init__(self, *paramsP, *paramsB):        super().__init__(*paramsP)        # Set up the specific paramsB    def get_paths(self, *params):        # specific implementation to childB我把它稱為:b = childB(...)b.load(...)(2) Implement another class, `AB`, with the following definition:```pythonclass AB(Parent):    def __init__(self, *paramsP):        super().__init__(*paramsP)    @abstract method    def get_paths(self, *params):        # To be implemented in childA and childB    def load(self, *params):        # Implements the load from childA and childB然后讓childA和childB都繼承自AB,并實(shí)現(xiàn)其特定的get_paths. 但是,這并沒有那么有效,因?yàn)槲沂盏藉e(cuò)誤:TypeError: Can't instantiate abstract class ChildB with abstract methods _AB__get_paths。重要的是要添加我無法實(shí)現(xiàn)load,Parent因?yàn)檫€有其他類(childC、childD、 ...)具有獨(dú)特的load實(shí)現(xiàn)。。我只想在childA和之間重用這段代碼childB那么,解決這個(gè)問題的正確方法是什么?
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

除非你想Parent用abstract繼承.load,否則直接把實(shí)現(xiàn)放進(jìn)去Parent。


如果.load僅對(duì)這兩個(gè)孩子來說很常見 - 您可以繼承Parent第三個(gè)孩子,例如LoadMixin繼承兩者Parent并混合


一種方法是:


class LoadableChild(Parent):

    def load(self, *params): ...


class childA(LoadableChild):

    def get_paths(self, *params): ...


class childB(LoadableChild):

    def get_paths(self, *params): ...

另一個(gè)是:


class LoadBase:

    def load(self, *params): ...


class childA(LoadBase, Parent):

    def get_paths(self, *params): ...


class childB(LoadBase, Parent):

    def get_paths(self, *params): ...

請(qǐng)注意后面方法中的繼承順序,如果您繼承父類作為第一個(gè)超類,則沒有簡(jiǎn)單的方法:


如果你的 mixin 繼承Parent– 沒有明確的 MRO

如果 mixin 繼承object- 抽象上存在實(shí)例化錯(cuò)誤.load。

我想說,這是偏好問題,對(duì)我個(gè)人來說,第一種方法更干凈


查看完整回答
反對(duì) 回復(fù) 2023-09-05
  • 1 回答
  • 0 關(guān)注
  • 271 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)