为什么我一直收到对象错误?

时间:2017-04-29 02:33:19

标签: python

我有以下程序,由于某种原因,我一直在

  

TypeError:object()不带参数

我是Python的绝对菜鸟,我甚至不确定我的编译器告诉我的是什么。

代码只是显示员工信息:

class Employee(object):


  def make_emp(self,name,idNumber,department,jobTitle):
    self.name = name
    self.idNumber = idNumber
    self.department = department
    self.jobTitle = jobTitle

 def displayEmployee(self):
    print("Name : ", self.name,  "idNumber: ",self.idNumber, "Department : ",   self.department, "Job Title : ", self.jobTitle)


emp1 = Employee("Susan Meyers",47899,"Accounting","Vice President")
emp2 = Employee("Mark Jones",39119,"IT","Programmer")
emp3 = Employee("Joy Rogers",81774,"Manufacturing","Engineer")


emp1.displayEmployee()
emp2.displayEmployee()
emp3.displayEmployee()

2 个答案:

答案 0 :(得分:2)

当您执行Employee(...)时,它会调用__init__,因此请将make_emp重命名为__init__

答案 1 :(得分:0)

您实际上不必使用__init__,并且可以使用静态方法支持多个初始化程序。

class Employee(object):
    @staticmethod
    def create(name, idNumber, department, jobTitle):
        e = Employee()
        e.name = name
        e.idNumber = idNumber
        e.department = department
        e.jobTitle = jobTitle
        return e

    def display(self):
        print("Name : ", self.name,  "idNumber: ",self.idNumber, "Department : ", self.department, "Job Title : ", self.jobTitle)

e = Employee.create("Susan Meyers", 47899, "Accounting", "Vice President")
e.display()

只要staticmethod返回一个新实例,就可以正常工作。所以你的想法并不遥远。