从父班覆盖传递的函数

发布于 2025-02-07 20:26:20 字数 540 浏览 2 评论 0原文

嘿,我的任务是创建ParentClass形状并将功能传递到不同的形状。由于电路计算采用不同的参数,因此我正在尝试覆盖该函数。它在警告后引发:“方法'circle.get_area()'的签名'在形状中不匹配签名”。这根本可能吗?我该怎么做?以后是否可以工作?

class Shape(ABC):
    @abstractmethod
    def get_area(self, x_l, y_l):
        pass

    def move(self, newx, newy):
        pass

class Circle(Shape):

    def __init__(self, rad, x_pos, y_pos):
        self.rad = rad
        self.pos = (x_pos, y_pos)
        self.area = self.get_area(self, self.rad)
    
    def get_area(self, rad):
        return(self.rad*self.rad*2*m.pi)

Hey my task is to create a parentclass Shape and pass the functions on to the different shapes. Since a Circlearea calculation takes different parameters I am trying to overwrite the function. It throws following warning: "Signature of method 'Circel.get_area()' does not match Signature in the base method in Shape". Is that possible at all? How would I have to do it? Should it work anyway later on?

class Shape(ABC):
    @abstractmethod
    def get_area(self, x_l, y_l):
        pass

    def move(self, newx, newy):
        pass

class Circle(Shape):

    def __init__(self, rad, x_pos, y_pos):
        self.rad = rad
        self.pos = (x_pos, y_pos)
        self.area = self.get_area(self, self.rad)
    
    def get_area(self, rad):
        return(self.rad*self.rad*2*m.pi)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文