我正在使用Visual Studio。我有两个Python代码文件,一个是我已导入的模块,一个是我的主类。我正在学习Python,有些事情我还不太了解。
有人可以向我解释我没有尽可能详细地了解什么?
我收到错误:“ TypeError:必须是str,而不是讲师”
我以为从Employee继承的Lecturer类将继承get_name方法?我正在使用super()函数将对象传递给构造函数。
from examplepackage.employee_lecturer import Employee
from examplepackage.employee_lecturer import Lecturer
employee_one = Employee("John")
print(employee_one.get_name())
lecturer_one = Lecturer("Emily", "Information Technology")
lecturer_one.print_information()
class Employee:
def __init__(self, n):
self.name = n
def get_name(self):
return self.name
def set_name(self, n):
self.name = n
class Lecturer(Employee):
def __init__(self, n, d):
super().__init__(self)
self.division = d
def set_division(self, div):
self.division = div
def get_division(self):
return self.division
def print_information(self):
print("Name:"+self.get_name())
将super
的init
中的Lecturer
更改为这样,
super().__init__(n)
您正在传递self
而不是参数n
,这就是为什么错误显示为TypeError: must be str, not Lecturer
。必须是n
的str而不是Lecturer
的self
]