Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python构造函数中缺少1个必需的位置参数_Python - Fatal编程技术网

python构造函数中缺少1个必需的位置参数

python构造函数中缺少1个必需的位置参数,python,Python,我试图学习python中的继承概念。我有员工类和衍生类高管 class Employee: 'Class defined for employee' def __init__(self, name, dept, salary): self.name = name self.dept = dept self.salary = salary 子类 class Executive(Employee): def __init_

我试图学习python中的继承概念。我有员工类和衍生类高管

class Employee:
    'Class defined for employee'

    def __init__(self, name, dept, salary):
        self.name = name
        self.dept = dept
        self.salary = salary
子类

class Executive(Employee):

    def __init__(self, name, dept, salary, hascar):
        Employee.__init__(name, dept, salary)
        self.hascar = hascar
has car是传递到构造函数的布尔值,但这会给我一个错误:

文件“I:\Python\u practicals\com\Python\oop\Executive.py”,第7行,在 init 员工。init(姓名、部门、薪资)类型错误:init()缺少1个必需的职位参数:“薪资”

当我尝试实例化Executive的对象时。

emp4=Executive(“Nirmal”,“Accounting”,150000,True)

虽然
\uuuu init\uuuu
是一个实例方法,但您在类上而不是在实例上调用它。此调用称为unbound,因为它未绑定到实例。因此,您需要显式地传递
self

class Executive(Employee):
    def __init__(self, name, dept, salary, hascar):
        Employee.__init__(self, name, dept, salary)
#                         ^^^^
        self.hascar = hascar
但是,建议的方法是使用:

返回一个代理对象,该对象将方法调用委托给类型为的父类或同级类。这对于访问类中已重写的继承方法非常有用

使用
super
时,您的代码如下所示:

class Executive(Employee):

    def __init__(self, name, dept, salary, hascar):
        super(Executive, self).__init__(name, dept, salary)
#       ^^^^^^^^^^^^^^^^^^^^^^
        self.hascar = hascar
Python3添加了一些语法糖,以简化此常见父类调用:

class Executive(Employee):

    def __init__(self, name, dept, salary, hascar):
        super().__init__(name, dept, salary)  # Py 3
#       ^^^^^^^
        self.hascar = hascar

虽然
\uuuu init\uuuu
是一个实例方法,但您是在类上而不是在实例上调用它。此调用称为unbound,因为它未绑定到实例。因此,您需要显式地传递
self

class Executive(Employee):
    def __init__(self, name, dept, salary, hascar):
        Employee.__init__(self, name, dept, salary)
#                         ^^^^
        self.hascar = hascar
但是,建议的方法是使用:

返回一个代理对象,该对象将方法调用委托给类型为的父类或同级类。这对于访问类中已重写的继承方法非常有用

使用
super
时,您的代码如下所示:

class Executive(Employee):

    def __init__(self, name, dept, salary, hascar):
        super(Executive, self).__init__(name, dept, salary)
#       ^^^^^^^^^^^^^^^^^^^^^^
        self.hascar = hascar
Python3添加了一些语法糖,以简化此常见父类调用:

class Executive(Employee):

    def __init__(self, name, dept, salary, hascar):
        super().__init__(name, dept, salary)  # Py 3
#       ^^^^^^^
        self.hascar = hascar
在Python 3.x中
使用
super()
关键字。这样可以避免显式键入基类。使代码更易于维护

class Executive(Employee):

    def __init__(self, name, dept, salary, hascar):
        super().__init__(name, dept, salary)
        self.hascar = hascar
在Python 3.x中
使用
super()
关键字。这样可以避免显式键入基类。使代码更易于维护

class Executive(Employee):

    def __init__(self, name, dept, salary, hascar):
        super().__init__(name, dept, salary)
        self.hascar = hascar

在这种情况下,python3
super()
的语法要简单得多
super()。\uuuu init\uuuuu(姓名、部门、薪水)
我知道,但是OP给问题添加了Python 2.7的标签,所以我从兼容的语法开始。哦,我还没有看到。很抱歉他还出于某种原因标记了python3.x,以防python3
super()
具有更简单的语法
super()。\uuuu init\uuuuu(姓名、部门、薪水)
我知道,但是OP给问题添加了Python 2.7的标签,所以我从兼容的语法开始。哦,我还没有看到。很抱歉出于某种原因,他还标记了Python3.x