Python错误:“finalStore”对象没有属性“name”

2024-04-26 22:56:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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)) 

我不知道是什么错误,但我有一个类似的程序,工作,我只是没有使用从*导入*

我是这样输出的

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, " "))

Tags: 对象nameselfprintstrljustlistinmarthree
1条回答
网友
1楼 · 发布于 2024-04-26 22:56:48

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

class finalStore(studStore):

    def __init__(self, name, ...):
        super(finalStore, self).__init__(name, marone, ...)

相关问题 更多 >