作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 Python 3.X 作为解释器。我刚刚继承了Employee
具有两个派生类的类为 Developer
和 Manager
.
下面的代码抛出一个类型错误:'__init__' requires a 'super' object but received a 'str'
.
我不明白为什么会发生这种情况,我还没有发现该程序有任何问题。
class Employee:
raise_amount=1.05
emp_count=0
def __init__(self,first_name,last_name, amount):
self.first_name=first_name
self.last_name=last_name
self.amount=amount
self.email_id="{0}.{1}@{1}.com" .format(first_name,last_name)
Employee.emp_count +=1
def fullname(self):
print ("%s %s"%(self.first_name,self.last_name))
def print1(self):
print (self.email)
print ("Total no of Employee are :%d" %(Employee.emp_count))
def raise_amount(self):
self.amount *=self.raise_amount
return self.amount
class Developer(Employee):
raise_amount = 1.10
def __init__(self,f,l,a,prog):
super.__init__(f,l,a)
self.programming=prog
class Manager(Employee):
def __init__(self,f,l,a,emp=None):
super.__init__(f,l,a)
if emp is None:
self.my_employee=[]
else:
self.my_employee=emp
def add_employee(self,emp):
if emp in self.my_employee:
print("employee is already exist")
else:
self.my_employee.append(emp)
def remove_employee(self,emp):
if emp in self.my_employee:
self.my_employee.remove(emp)
def print_employee(self):
for emp in self.my_employee:
print (emp.fullnamme())
dev1=Developer("subhendu","panda",500000,"Python")
dev1.raise_amount()
dev2=Developer('Aditya','bishoyi',5688989,'java')
dev2.fullname()
dev1.fullname()
emp1=Employee("tonu","trip",30000)
emp1.raise_amount()
emp1.fullname()
mgr1=Manager("Biplab","choudhury",5000000)
mgr1.fullname()
mgr1.add_employee(dev1)
mgr1.add_employee(emp1)
mgr1.add_employee(dev2)
mgr1.print_employee()
mgr1.remove_employee(dev1)
mgr1.print_employee()
Traceback (most recent call last):
File "C:/Users/Subhendu/PycharmProjects/hello/inheritance.py", line 56, in
<module> dev1=Developer("subhendu","panda",500000,"Python")
File "C:/Users/Subhendu/PycharmProjects/hello/inheritance.py", line 27, in
init super.__init__(f,l,a) TypeError: descriptor 'init' requires a 'super'
object but received a 'str'
最佳答案
您的 super
调用在 Developer
中给出错误类应该是:
super(Developer, self).__init__(f, l, a)
Manager
类(class):
super(Manager, self).__init__(f, l, a)
Employee
中您有两个名为
raise_amount
的属性,一个是
float
一个是方法(函数)。这是不允许的,
float
优先,所以
dev1.raise_amount()
失败。
print_employee()
你拼错了
emp.fullname
关于python - 类型错误 : descriptor '__init__' requires a 'super' object but received a 'str' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353738/
我是一名优秀的程序员,十分优秀!