Python:继承和超级调用有问题
Python: Trouble with inheritance and super call
我正在尝试学习 python(之前的编程经验很少),但在 codeacademy 上遇到了这个问题,关于在子 class 中覆盖它之后从基础 class 调用方法=].
这是代码:
class Employee(object):
"""Models real-life employees!"""
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 20.00
class PartTimeEmployee(Employee):
def calculate_wage(self, hours):
self.hours = hours
return hours * 12.00
def full_time_wage(self, hours):
super(PartTimeEmployee, self).calculate_wage(self, hours)
milton = PartTimeEmployee("Milton")
print(milton.full_time_wage(10))
不幸的是,它抛出以下内容:
Traceback (most recent call last):
File "python", line 20, in <module>
File "python", line 17, in full_time_wage
TypeError: calculate_wage() takes exactly 2 arguments (3 given)
所以某处交了一个变量 too many,但是,我想不通在哪里。
更新 1:
通过在第 17 行仅传递小时解决了 Traceback 调用。
但是,现在我的调用结果
print(milton.full_time_wage(10))
给我 None
而不是我期望的 200。
有人可以告诉我我做错了什么吗?
谢谢
该行需要是:
return super(PartTimeEmployee, self).calculate_wage(hours)
方法调用已经通过self。您必须 return 调用该方法的结果。
问题是您传递给函数的第二个 self
。
super(PartTimeEmployee, self).calculate_wage(self, hours)
^
当您将 self
传递给 super
时,它会自动将超级 class 传递给该函数(如果它提供该函数)。否则它将在您的 classes __mro__
.
的其他基础中寻找函数
在文档中阅读更多内容:https://docs.python.org/3.6/library/functions.html#super
您不需要调用 calculate_wage() 并将 self 作为参数之一:
应该是:
class PartTimeEmployee(Employee):
def calculate_wage(self, hours):
self.hours = hours
return hours * 12.00
def full_time_wage(self, hours):
super(PartTimeEmployee, self).calculate_wage(hours)
因此,上述所有答案都解决了错误消息。
我的代码没有返回任何内容的问题是我忘记了第 17 行的 return
调用 - 我是个白痴..
我正在尝试学习 python(之前的编程经验很少),但在 codeacademy 上遇到了这个问题,关于在子 class 中覆盖它之后从基础 class 调用方法=].
这是代码:
class Employee(object):
"""Models real-life employees!"""
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 20.00
class PartTimeEmployee(Employee):
def calculate_wage(self, hours):
self.hours = hours
return hours * 12.00
def full_time_wage(self, hours):
super(PartTimeEmployee, self).calculate_wage(self, hours)
milton = PartTimeEmployee("Milton")
print(milton.full_time_wage(10))
不幸的是,它抛出以下内容:
Traceback (most recent call last):
File "python", line 20, in <module>
File "python", line 17, in full_time_wage
TypeError: calculate_wage() takes exactly 2 arguments (3 given)
所以某处交了一个变量 too many,但是,我想不通在哪里。
更新 1:
通过在第 17 行仅传递小时解决了 Traceback 调用。
但是,现在我的调用结果
print(milton.full_time_wage(10))
给我 None
而不是我期望的 200。
有人可以告诉我我做错了什么吗?
谢谢
该行需要是:
return super(PartTimeEmployee, self).calculate_wage(hours)
方法调用已经通过self。您必须 return 调用该方法的结果。
问题是您传递给函数的第二个 self
。
super(PartTimeEmployee, self).calculate_wage(self, hours)
^
当您将 self
传递给 super
时,它会自动将超级 class 传递给该函数(如果它提供该函数)。否则它将在您的 classes __mro__
.
在文档中阅读更多内容:https://docs.python.org/3.6/library/functions.html#super
您不需要调用 calculate_wage() 并将 self 作为参数之一:
应该是:
class PartTimeEmployee(Employee):
def calculate_wage(self, hours):
self.hours = hours
return hours * 12.00
def full_time_wage(self, hours):
super(PartTimeEmployee, self).calculate_wage(hours)
因此,上述所有答案都解决了错误消息。
我的代码没有返回任何内容的问题是我忘记了第 17 行的 return
调用 - 我是个白痴..