TypeError:\uyu init_uu()接受4个位置参数,但给出了7个

2024-09-30 10:37:11 发布

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

class Employee(object):
    def __init__(self,ename,salary,dateOfJoining):
        self.ename=ename
        self.salary=salary
        self.dateOfJoining=dateOfJoining
    def output(self):
        print("Ename: ",self.ename,"\nSalary: ",self.salary,"\nDate Of 
Joining: ",self.dateOfJoining)
class Qualification(object):
    def __init__(self,university,degree,passingYear):
        self.university=university
        self.degree=degree
        self.passingYear=passingYear
    def qoutput(self):
        print("Passed out fom University:",self.university,"\nDegree:",self.degree,"\Passout year: ",self.passingYear)
class Scientist(Employee,Qualification):
    def __int__(self,ename,salary,dateOfJoining,university,degree,passingYear):
        Employee.__init__(self,ename,salary,dateOfJoining)
        Qualification.__init__(self,university,degree,passingYear)
    def soutput(self):
        Employee.output()
        Qualification.output()
a=Scientist('Ayush',20000,'21-04-2010','MIT','B.Tech','31-3-2008')
a.soutput()

我无法找到问题的解决方案,也无法理解为什么会发生这种错误。我是python新手。谢谢


Tags: selfoutputobjectinitdefemployeeclassprint
2条回答

您的Scientist类构造函数拼写错误为__int__,而不是{}。由于Scientist类中没有可重写的构造函数,因此它在继承链上向上一级,并使用雇员的构造函数,实际上它只使用4个位置参数。只要修正一下打字错误就行了。在

(您在代码中对类做了一些其他不好的事情,但我允许其他人使用提示进行评论)

你的科学家类的init函数写为:

def __int__

而不是

^{pr2}$

所以它从其父类继承了init函数,它接收的参数比发送给类的要少。在

另外,您应该使用super函数,而不是调用父函数的init。在

super(PARENT_CLASS_NAME, self).__init__()

这显然适用于所有父函数。在

相关问题 更多 >

    热门问题