簡體   English   中英

python如何抽象出成員變量的class

[英]python how to abstract the class of member variable

假設我有一個像下面這樣的模塊

class B:
  def ...
  def ...

class A:
  b: B

  def ...
  def ...

我只使用 class B 作為 class A 的成員變量

當我嘗試為我的業務邏輯抽象這個模塊時,我該怎么辦?

  1. 一個大接口,具有 class A 和 class B 的抽象方法
  2. 兩個接口,分別具有 class A 和 class B 的抽象方法
  3. 以上都是錯誤的。 另一種方式

1 和 2 都是正確的方法,但這完全取決於您的應用程序。

我認為,當您的兩個類具有單獨的工作並且彼此完全不同時,兩個接口分別具有 class A 和 class B 的抽象方法是正確的方法。

But, as you have mentioned in your code that you have inherited class B in class A. If you create a single interface for class A, it will also allow you to access the methods from class B. So, this approach is good. 此外,這種方法將縮短代碼的長度,從而加快處理速度。

我希望這能幫助你做出決定。 讓我知道是否需要任何其他澄清。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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