我是面向对象的新手。 我正在尝试测试RightPyramid初始化方法,不确定是否有办法像我一样 RightPyramid init没有任何参数。
from typing import List
from abc import ABCMeta, abstractmethod
class Triangle(metaclass=ABCMeta):
def __init__(self, base: int, height: int) -> None:
assert base >= 0, "base must be greater or equal to 0"
assert height >= 0, "height must be greater or equal to 0"
self.base = base
self.height = height
@abstractmethod
def area(self) -> int:
pass
class RightPyramid(Triangle):
def __init__(self):
Triangle.__init__(self, 3, 2)
def area(self):
return 0.5 * self.base + self.height
执行以下代码:
会实际调用RightPyramid .__ init __()方法,并将self替换为实例并在rp变量中返回。