【问题标题】:type error when using class inheritence使用类继承时出现类型错误
【发布时间】:2016-05-15 13:18:23
【问题描述】:

我有以下代码:

class SphericalRefraction(OpticalElement):
    def __init__(self, r0, normal, curvature, n, h):
        self._r0 = r0
        self._normal = normal/npl.norm(normal)
        self._curvature = curvature
        self._n = n
        self._aperture = h

def OutputPlane(SphericalRefraction):
    def __init__(self, r0, normal, h=15):
        SphericalRefraction.__init__(self, r0=r0, normal=normal, curvature=0, n=1, h=h)

但是当我在 main 中构造一个类 OutputPlane 时:

screen = r.OutputPlane(np.array([0,0,f]),np.array([0,0,1]), 5)

我有以下错误:

TypeError: OutputPlane() takes exactly 1 argument (3 given)

我做错了什么?我应该怎么做才能从 SphericalRefraction 继承 OutputPlane?

【问题讨论】:

  • r.OutputPlane(.. 中的 r 是什么?
  • 您没有显示足够的代码。
  • 你的OutputPlane 是一个函数(用def 声明)。我猜你想使用class

标签: python class oop inheritance typeerror


【解决方案1】:

变化:

def OutputPlane(SphericalRefraction):

进入:

class OutputPlane(SphericalRefraction):

你应该很高兴。

【讨论】:

    【解决方案2】:

    您没有继承 SphericalRefraction,因为您将 OutputPlane 定义为函数而不是类。

    所以def OutputPlane(SphericalRefraction): 应该 class OutputPlane(SphericalRefraction):

    【讨论】:

    • 我犯了一个多么愚蠢的错误...谢谢您的帮助
    猜你喜欢
    • 2021-06-05
    • 2016-03-12
    • 2011-07-04
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多