我对这个Python类不了解什么?

问题描述 投票:-1回答:1

我正在使用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())
python python-3.x
1个回答
2
投票

superinit中的Lecturer更改为这样,

super().__init__(n)

您正在传递self而不是参数n,这就是为什么错误显示为TypeError: must be str, not Lecturer。必须是n的str而不是Lecturerself]

© www.soinside.com 2019 - 2024. All rights reserved.