【发布时间】:2021-10-19 10:25:51
【问题描述】:
我有一个关于多级继承的问题。 我正在尝试编写以下形式的类:
from abc import ABC, abstractmethod
import numpy as np
### Parent class
class A(ABC):
@abstractmethod
def eval(self, x: np.ndarray) -> np.ndarray:
pass
@abstractmethod
def func(self, x: np.ndarray) -> None:
pass
### 1. Inheritance
class B1(A):
def eval(self, x: np.ndarray) -> np.ndarray:
#do something here
return np.zeros(5)
@abstractmethod
def func(self, x: np.ndarray) -> None:
pass
class B2(A):
def eval(self, x: np.ndarray) -> np.ndarray:
#do something different here
return np.zeros(10)
@abstractmethod
def func(self, x: np.ndarray) -> None:
pass
### 2. Inheritance
class C1(B1):
def func(self, x: np.ndarray) -> None:
print('child1.1')
class C2(B1):
def func(self, x: np.ndarray) -> None:
print('child1.2')
class C3(B2):
def func(self, x: np.ndarray) -> None:
print('child2.1')
c1 = C1()
c2 = C2()
c3 = C3()
我不打算实例化 A B1 或 B2。
我的问题是,如果这是在 python 中解决这个问题的正确方法?我想明确一点,Bx 仍然是抽象类
【问题讨论】:
标签: python python-3.x class abc