0

我正在使用 python 中的继承,但我遇到了一个错误,我不知道如何修复,'finalStore' 对象没有属性'marone'。当我尝试创建一个对象时,我得到了这个。

from ClassFile import studStore

class finalStore (studStore):

    grandAve = 0
    numStu = 0

    def __init__(self, name, marone, martwo, marthree, marfour, corone, cortwo, corthree, corfour):
        studStore.__init__(self, name, marone, martwo, marthree, marfour)
        self.corone = corone
        self.cortwo = cortwo
        self.corthree = corthree
        self.corfour = corfour
        finalStore.numStu += 1
        self.holder = finalStore.numStu
        self.average = (marone + martwo + marthree + marfour)/4
        finalStore.grandAve += self.average
        self.storit = finalStore.grandAve

我为子类初始化

class studStore:

    def __init__(self, name, marone, martwo, marthree, marfour):
        self.newname = name
        self.Ave = 0
        self.marone = marone
        self.martwo = martwo
        self.marthree = marthree
        self.marfour = marfour

以及父类的初始化。我的主线只是一个循环,我在其中创建了多个对象,但在这一行出错:

listIn.append(finalStore(name, gradeone, gradetwo, gradethree, gradefour, courseOne, courseTwo, courseThree, courseFour)) 

我不确定错误是什么,但我有一个类似的程序可以工作,我只是没有使用 from * import *

我这样输出

for i in range (0,len(listIn)):
    print(str(listIn[i].returnName()).ljust(20," "), end = " ")
    print(str(listIn[i].returnOne()).ljust(20, " "))
    print(str(listIn[i].returnTwo()).ljust(20, " "))
    print(str(listIn[i].returnThree()).ljust(20, " "))
    print(str(listIn[i].returnFour()).ljust(20, " "))
4

1 回答 1

0

您对超类的 init 函数的调用不正确。这是您应该如何做的:

class finalStore(studStore):

    def __init__(self, name, ...):
        super(finalStore, self).__init__(name, marone, ...)
于 2013-04-19T18:02:23.217 回答