亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在python中,如何讓兩個類共享一個方法的實現,該方法調用另一個方法以及每個類的特定實現

在python中,如何讓兩個類共享一個方法的實現,該方法調用另一個方法以及每個類的特定實現

喵喵時光機 2023-09-05 17:26:49
假設我有一個父類Parent,其抽象方法load在其子類中實現?,F在,我有兩個子類childA和childB,都繼承自Parent。loadfor的實現childA和childB完全相同。但是 的此實現load調用另一個方法 ,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中的代碼,但實現特定于每個類?我嘗試了兩種方法:childAchildBget_paths(1) 繼承childB自childA,并覆蓋 的實現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,并實現其特定的get_paths. 但是,這并沒有那么有效,因為我收到錯誤:TypeError: Can't instantiate abstract class ChildB with abstract methods _AB__get_paths。重要的是要添加我無法實現load,Parent因為還有其他類(childC、childD、 ...)具有獨特的load實現。。我只想在childA和之間重用這段代碼childB那么,解決這個問題的正確方法是什么?
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

除非你想Parent用abstract繼承.load,否則直接把實現放進去Parent。


如果.load僅對這兩個孩子來說很常見 - 您可以繼承Parent第三個孩子,例如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): ...

另一個是:


class LoadBase:

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


class childA(LoadBase, Parent):

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


class childB(LoadBase, Parent):

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

請注意后面方法中的繼承順序,如果您繼承父類作為第一個超類,則沒有簡單的方法:


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

如果 mixin 繼承object- 抽象上存在實例化錯誤.load。

我想說,這是偏好問題,對我個人來說,第一種方法更干凈


查看完整回答
反對 回復 2023-09-05
  • 1 回答
  • 0 關注
  • 283 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號