错误的结果打印到控制台
wrong result printed to the console
我有以下代码,它应该将 3500.0 打印到控制台:
class Employee:
""" Creates an instance of Employee """
def __init__(self, name, annual_salary):
self.name = name
self.annual_salary = annual_salary
def calculate_monthly_salary(self):
return annual_salary / 0.12
class CustomerServiceEmployee(Employee):
""" Creates an instance of CustomerServiceEmployee """
def __init__(self, name, annual_salary, department):
super().__init__(name, annual_salary)
self.department = department
cs_manager = CustomerServiceEmployee("Kelly Johnson", 42000, "Customer Service")
kellys_monthly_salary = Employee.calculate_monthly_salary
print(kellys_monthly_salary)
而是打印:
<function Employee.calculate_monthly_salary at 0x7fdfb1db31f0>
我相信我的问题出在方法 calculate_monthly_salary 上,但我终究无法解决。有人可以解释一下吗?
首先,您已经为 CustomerServiceEmployee
创建了一个对象并将其存储在 cs_manager
中。那为什么不用呢?
kellys_monthly_salary = cs_manager.calculate_monthly_salary()
也line 10
应该有self
。
self.annual_salary
完整代码
class Employee:
""" Creates an instance of Employee """
def __init__(self, name, annual_salary):
self.name = name
self.annual_salary = annual_salary
def calculate_monthly_salary(self):
return self.annual_salary / 0.12
class CustomerServiceEmployee(Employee):
""" Creates an instance of CustomerServiceEmployee """
def __init__(self, name, annual_salary, department):
super().__init__(name, annual_salary)
self.department = department
cs_manager = CustomerServiceEmployee("Kelly Johnson", 42000, "Customer Service")
kellys_monthly_salary = cs_manager.calculate_monthly_salary()
print(kellys_monthly_salary)
我有以下代码,它应该将 3500.0 打印到控制台:
class Employee:
""" Creates an instance of Employee """
def __init__(self, name, annual_salary):
self.name = name
self.annual_salary = annual_salary
def calculate_monthly_salary(self):
return annual_salary / 0.12
class CustomerServiceEmployee(Employee):
""" Creates an instance of CustomerServiceEmployee """
def __init__(self, name, annual_salary, department):
super().__init__(name, annual_salary)
self.department = department
cs_manager = CustomerServiceEmployee("Kelly Johnson", 42000, "Customer Service")
kellys_monthly_salary = Employee.calculate_monthly_salary
print(kellys_monthly_salary)
而是打印:
<function Employee.calculate_monthly_salary at 0x7fdfb1db31f0>
我相信我的问题出在方法 calculate_monthly_salary 上,但我终究无法解决。有人可以解释一下吗?
首先,您已经为 CustomerServiceEmployee
创建了一个对象并将其存储在 cs_manager
中。那为什么不用呢?
kellys_monthly_salary = cs_manager.calculate_monthly_salary()
也line 10
应该有self
。
self.annual_salary
完整代码
class Employee:
""" Creates an instance of Employee """
def __init__(self, name, annual_salary):
self.name = name
self.annual_salary = annual_salary
def calculate_monthly_salary(self):
return self.annual_salary / 0.12
class CustomerServiceEmployee(Employee):
""" Creates an instance of CustomerServiceEmployee """
def __init__(self, name, annual_salary, department):
super().__init__(name, annual_salary)
self.department = department
cs_manager = CustomerServiceEmployee("Kelly Johnson", 42000, "Customer Service")
kellys_monthly_salary = cs_manager.calculate_monthly_salary()
print(kellys_monthly_salary)