cost 73 ms
mixin 中的方法取決於 mixin 到的 class 的方法時的情況

[英]Case when method in mixin depends on a method of the class that mixin-ed to

當 mixin 中的方法依賴於 class 的 mixin 方法時,我想問一下設計模式。 下面的例子是 python,但我相信其他語言也會出現這個問題。 例如,假設我有以下兩個混入,我想注入一些 class。在下面的代碼中,我想注入f但f要求 class 混入來實現g ,因為g將是用於ffrom a ...

C++,父母同祖的孩子

[英]C++, child with both parents having a same ancestor

我現在遇到 C++ 類和 inheritance 的問題... 假設我們有 每當我創建D時,它都會調用B的構造函數和C的構造函數,這會導致A的多個實例。 然后編譯器說它不知道應該調用哪個“函數”,是來自B的那個還是來自C的那個。 我想知道如何只調用A構造函數一次,然后用它來構建B和C 。 我已經嘗試 ...

了解 python 的多個 inheritance 中的 MRO、構造函數和方法

[英]understanding MRO, constructors, and methods in multiple inheritance of python

以下是兩組代碼:一組具有構造函數__init__() ,另一組具有display()方法。 代碼一: 如果我們編譯並運行上面的這組代碼,output 將是: 現在,第二個代碼塊在下面, 如果在上面的第二個代碼塊中,我們將__init__()替換為display()方法,那么,由於class B中的 ...

Python MRO 簡明英語

[英]Python MRO in plain English

我是 Python 的新手,無法理解 Python MRO。 有人可以盡可能簡單地解釋下面的問題嗎? 為什么這段代碼拋出TypeError: Cannot create a consistent method resolution : 雖然這段代碼工作正常: ...

將 **kwargs arguments 傳遞給多個 inheritance 中的父類會丟失 kwargs 內容

[英]Passing **kwargs arguments to parent classes in multiple inheritance loses kwargs content

在多個inheritance的場景下,Python3是如何將arguments傳遞給所有父類的? 考慮下面的玩具程序: output 是: 我期望做的是將相同的 kwargs 傳遞給所有父類,並讓他們根據需要使用這些值。 但是,我看到的是,一旦我關閉了第一個父 class A ,kwargs 就被 ...

Python:重疊(非獨占)繼承以提供基於實例參數的方法

[英]Python: overlapping (non exclusive) inheritance to have methods available based on instance parameters

如果參數滿足特定條件,我希望某些屬性和方法僅在類實例中可用。 不同的情況不是唯一的。 我已經有了一個可行的解決方案(包括 ShadowRanger 的建議): 屬性和方法的可用性取決於實例參數: 基類的屬性和方法應該始終可用。 如果第一個參數等於 3,則子類 _triangle 的屬性和方法應該可用 ...

如何組合python中不同父類定義的屬性?

[英]How to combine properties defined by different parent classes in python?

假設我們有兩個類,每個類stuff以不同的方式計算屬性。 是否可以將它們的輸出組合到子類的方法/屬性中? 下面的代碼顯示了預期的效果(盡管get_stuff_from需要用適當的 python 結構替換,如果存在的話)。class Foo(): @property def stuf ...

多個 class inheritance 一個祖父母,兩個父母,一個孩子的類型錯誤 class

[英]Multiple class inheritance TypeError with one grandparent, two parents, one child class

我正在練習 OOP 並不斷遇到這個問題。 這是一個例子。 拿一個菱形倍數class inheritance排列,帶Weapon feeding Edge和Long,都是雙漢德繼承的。 如果我在不繼承 Weapon 的情況下對 Edge 進行編碼,則代碼可以正常工作。 但是一旦我將 Weapon 作 ...

使用多個時指針偏移不正確 inheritance

[英]Pointer offset is incorrect when using multiple inheritance

我在訪問成員變量時使用多個 inheritance 的某些代碼中遇到錯誤。 不幸的是,我無法提供您可以運行的最小可重現示例,但我可以提供一堆關於我所看到的信息。 我的代碼使用 GCC 編譯為 ARM。 粗略地說,這就是我所擁有的(不是我的實際代碼): 其中類 A、B 和 C 也從其他類多重繼承。 ...

多個 Inheritance 和橋接模式

[英]Multiple Inheritance and Bridge Pattern

使用多個 inheritance 實現橋接模式有什么意義? GoF 在他們的《設計模式》一書中解釋它的方式使它看起來與意圖有悖常理。 另外,為什么多個 inheritance 使實現不符合真正的橋接模式? 非常感謝示例。 ...

如何在 Python 多重繼承中創建非覆蓋方法存根?

[英]How to make a non-overriding method stub in Python multi-inheritance?

假設您有 2 個混合類,每個類都定義了抽象方法和實現。 他們一起實現了每個方法,但根據 inheritance 的順序,空存根將覆蓋另一個 class 的實現。在大多數情況下至少有兩種方法可以解決這個問題,但我不太喜歡這兩種方法。 可以刪除抽象方法,只依賴鴨子類型,但沒有明確的接口定義和類型提示。 ...

在多個 inheritance - Python 中從父 class 調用特定方法

[英]Call specific method from parent class in multiple inheritance - Python

我有一個 class 和多個 inheritance。我想從一些父母的同名方法中連接 output。 理想情況下,我可以在不遍歷所有父 class 的情況下執行此操作,而是明確選擇我想要的案例。class my_class1: def common_method(self): return ...

為什么在使用 Inheritance 時會出現錯誤

[英]Why does this give me an error when using Inheritance

我目前正在學習 Inheritance。 下面的代碼工作得很好。 但是,下面的代碼不起作用。 錯誤我只在這兩個代碼片段之間更改了一件事,就是在 class Employee 旁邊添加了“public Student”。 我做錯了什么? 請使用簡單的文字/邏輯來解釋這一點。 ...

python 中的多個 inheritance 的 AttributeError 問題

[英]AttributeError Problem with Multiple inheritance in python

我想計算 python 中具有多重繼承的學生的總分和平均分。 但是,每當我為我的孩子 class 創建一個 object 時,它讓我可以訪問父類的所有方法,但是當我調用孩子 class 的方法時會顯示 AttributeError。 我嘗試使用 super() function 但結果是一樣的。 我 ...

Python 的 Super() 如何在多個 inheritance 中為超類工作?

[英]How Python's Super() works in multiple inheritance for superclasses?

我試圖了解 super 如何在 python 的多個 inheritance 中為超類工作,例如在下面的代碼中: 如果我刪除super().__init__(**kwargs)執行結束 '第三' object 沒有屬性 'parm2' 僅打印parm1和parm3 ,即使我在Class Third ...


 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM