1

所以我在python中的继承遇到了一些麻烦。我有两节课。第一个是

class Base1DHeatEquation:
    def __init__(self, alpha, final_time, time_discrete):
       self.alpha = alpha
       self.final_time = final_time
       self.time_discrete = time_discrete

       #Additional Functions which aren't causing a problem

第二个是继承第一个的类

class IntialValueTest1DHE(Base1DHeatEquation):
   def __init__(self, alpha, final_time, time_discrete,intialValues,\
             x_discrete ,noise):
        super(IntialValueTest1DHE,self).__init__(self, alpha, final_time, time_discrete)
        self.intialValues = intialValues

   #Additional Functions which aren't causing a problem

问题是当我尝试创建 IntialValueTest1DHE 对象时,我得到以下信息

>>> import HeatEquation1D as he #The File where both classes are stored
>>> temp = he.IntialValueTest1DHE(1,1,100,np.sin,100,0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes exactly 4 arguments (7 given)

似乎对象创建正在尝试从父类而不是子类访问init,我不知道如何解决这个问题。

4

1 回答 1

4

您不需要在 super() 声明中添加“self”。此外,您应该使用“新风格”类:

class Base1DHeatEquation(object):
    def __init__(self, alpha, final_time, time_discrete):
       self.alpha = alpha
       self.final_time = final_time
       self.time_discrete = time_discrete

class IntialValueTest1DHE(Base1DHeatEquation):
    def __init__(self, alpha, final_time, time_discrete,intialValues,
           x_discrete ,noise):
        super(IntialValueTest1DHE,self).__init__(alpha, final_time, time_discrete)
        self.intialValues = intialValues
于 2013-11-13T21:46:49.413 回答