面向对象的python编程

时间:2018-02-08 05:07:05

标签: python

我正在使用python进行面向对象编程,我将值传递给参数,但它给了我错误。我正在使用idle ide

这就是我所做的

class employee:
  def __int__(self,first,last,pay):
    self.first = first
    self.last = last
    self.pay = pay
    self.email = first + last +'@company.com'


>>> emp_1 = employee('aditi','dabre',50000)
   Traceback (most recent call last):
   File "<pyshell#9>", line 1, in <module>
   emp_1 = employee('aditi','dabre',50000)
  TypeError: object() takes no parameters
>>> emp = employee()
>>> print(emp)
<__main__.employee object at 0x00000242D36BD7B8>

2 个答案:

答案 0 :(得分:1)

正如DYZ所说,您需要将def __int__更改为def __init__。此函数是初始化的缩写。您可以看到您的代码使用下面的修改。

class employee:
    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + last +'@company.com'

emp_1 = employee('aditi','dabre',50000)
print(emp_1)

输出:<__main__.employee instance at 0x10122ebd8>

答案 1 :(得分:0)

你拼错了__init__emp = employee()工作的原因是employee类将继承基类的__init__(在这种情况下为object)。由于它没有任何参数,因此您对emp = employee()的调用工作正常。

相关问题