Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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 如何仅继承父类的4个属性中的2个?_Python - Fatal编程技术网

Python 如何仅继承父类的4个属性中的2个?

Python 如何仅继承父类的4个属性中的2个?,python,Python,我得到这个错误: TypeError:init()缺少2个必需的位置参数:“Description”和“precio” 我不知道为什么会发生这种情况,“Alimentos”类工作得很好,我想这是因为我继承了父类“Producto”的每一个属性。然后“Libros”和“Medicamentos”这两个类出现了问题,在Pycharm中,我从这些行中得到了以下信息: 参数“description”未填充 参数“precio”未填充 希望你能帮助我 def __init__(self, refe

我得到这个错误: TypeError:init()缺少2个必需的位置参数:“Description”和“precio”

我不知道为什么会发生这种情况,“Alimentos”类工作得很好,我想这是因为我继承了父类“Producto”的每一个属性。然后“Libros”和“Medicamentos”这两个类出现了问题,在Pycharm中,我从这些行中得到了以下信息:

参数“description”未填充 参数“precio”未填充

希望你能帮助我

    def __init__(self, referencia, nombre, descripcion, precio):
        self.referencia = referencia
        self.nombre = nombre
        self.descripcion = descripcion
        self.precio = precio

    def precio_mayor_10(self):
        if self.precio > 10:
            print("Este producto tiene un precio mayor a 10")


class Alimentos(Producto):
    def __init__(self, referencia, nombre, descripcion, precio, productor, distribuidor):
        #This Super works fine
        super().__init__(referencia, nombre,descripcion, precio)

        self.productor = productor
        self.distribuidor = distribuidor

class Libros(Producto):
    def __init__(self, referencia, precio, nombre_libro, nombre_autor, isbn, distribuidor):
        #Here I want to inherit only the "referencia" and "precio" attributes
        super().__init__(referencia, precio)

        self.nombre_libro = nombre_libro
        self.nombre_autor = nombre_autor
        self.isbn = isbn
        self.distribuidor = distribuidor

class Medicamentos(Producto):
    def __init__(self, nombre, precio, distribuidora, farmaceutica):
        #Here I want to inherit only the "referencia" and "precio" attributes
        super().__init__(nombre, precio)

        self.distribuidora = distribuidora
        self.farmaceutica = farmaceutica


Alimento1 = Alimentos(4343, "papa","Tubérculo", 2, "ProductorXYZ", "DistribuidorXYZ")
Alimento2 = Alimentos(4923, "Zanahoria","Hortaliza", 11, "ProductorXYZ", "DistribuidorXYZ")
Alimento3 = Alimentos(9343, "Caraota","Legumbre", 10, "ProductorXYZ", "DistribuidorXYZ")

Alimento1.precio_mayor_10()
Alimento2.precio_mayor_10()
Alimento3.precio_mayor_10()

Libro1 = Libros(3434, 50, "Harry Potter", "J. K. Rowling", "843843", "DistribuidoraXYZ")
Libro2 = Libros(7424, 9, "Pepito el alegre", "Juan Pérez", "390402", "DistribuidoraXYZ")

Libro1.precio_mayor_10()
Libro2.precio_mayor_10()

Medicamento1 = Medicamentos("Aspirina", 15, "DistribuidoraXYZ", "FarmaceuticaXYZ" )

Medicamento1.precio_mayor_10()


不幸的是,当您继承父类时,会得到所有属性。 在我看来,您有几种选择:

  • 为“可选”字段提供父类默认值,并将其移动到属性列表的末尾
  • 添加中间级别的继承,并更新基本级别,使其不需要2个可选属性
  • 将值设置为“无”以仅“使其工作”[这不是首选,因为它可能导致意外行为]

必须传递所有没有默认值的参数。我只是展示如何让它工作,而不是最佳实践

class Libros(Producto):
    def __init__(self, referencia, precio, nombre_libro, nombre_autor, isbn, distribuidor):
        #Here I want to inherit only the "referencia" and "precio" attributes
        super().__init__(referencia, None, None, precio)

class Medicamentos(Producto):
    def __init__(self, nombre, precio, distribuidora, farmaceutica):
        #Here I want to inherit only the "referencia" and "precio" attributes
        super().__init__(None, nombre, None, precio)

您总是继承所有属性。
产品
构造函数需要4个参数,您必须提供它们。通常,您可以使用默认值,但这些参数都是位置参数,您需要的参数不是第一个参数。你必须说
super()。\uuuu init\uuu(None,nombre,None,precio)
@TimRoberts,或者使用关键字参数而不是位置参数(将默认值设置为参数后)。这与属性无关。它是关于init构造函数的参数列表。(必须将参数传递给它期望的超类init。如果不使用它们,可以传递伪值。(顺便说一句,示例代码缺少顶部的
类Producto:
行)。