创建类时,什么时候需要使用 init()?
对于下面的代码,当我们创建 PartTimeEmployee() 类时,我们可以只从 Employee() 类继承 init() 吗?还是我们应该重新输入?
我发现两个代码都有效:
class Employee(object):
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
和
class Employee(object):
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 __init__(self,employee_name):
self.employee_name = employee_name
def calculate_wage(self,hours):
self.hours = hours
return hours * 12.00
这里有某种最佳实践吗?