TypeError: __init__() takes 4 positional arguments but 7 were given
TypeError: __init__() takes 4 positional arguments but 7 were given
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()
我无法找到问题的解决方案,也无法理解为什么会出现此 TpyeError。我是 python 的新手。谢谢
您的科学家 class 的 init 函数写为:
def __int__
而不是
def __init__
所以发生的事情是它从其父 class 继承了 init 函数,它接收的参数比您发送到 class 的参数少。
此外,您应该使用 super 函数,而不是调用父级的 init。
super(PARENT_CLASS_NAME, self).__init__()
这显然适用于所有父函数。
您的科学家 class 构造函数被错误拼写为 __int__
而不是 __init__
。由于科学家 class 没有使用覆盖的构造函数,它在继承链上上升了一个级别并使用了 Employee 的构造函数,实际上它只使用了 4 个位置参数。只需更正拼写错误即可。
(您在代码中用 classes 做的其他一些不好的事情,但我会允许其他人评论提示)
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()
我无法找到问题的解决方案,也无法理解为什么会出现此 TpyeError。我是 python 的新手。谢谢
您的科学家 class 的 init 函数写为:
def __int__
而不是
def __init__
所以发生的事情是它从其父 class 继承了 init 函数,它接收的参数比您发送到 class 的参数少。
此外,您应该使用 super 函数,而不是调用父级的 init。
super(PARENT_CLASS_NAME, self).__init__()
这显然适用于所有父函数。
您的科学家 class 构造函数被错误拼写为 __int__
而不是 __init__
。由于科学家 class 没有使用覆盖的构造函数,它在继承链上上升了一个级别并使用了 Employee 的构造函数,实际上它只使用了 4 个位置参数。只需更正拼写错误即可。
(您在代码中用 classes 做的其他一些不好的事情,但我会允许其他人评论提示)