super().__init__(model, year, position) TypeError: __init__() 缺少 1 个必需的位置参数:'position'

时间:2020-12-20 14:20:59

标签: python

我有问题。像那样! 代码:

class Pass:
    def __init__(self, model, color, year, position):
        self.model = model
        self.color = color
        self.year = year
        self.position = position

class Car(Pass):
    def __init__(self, model, year, position):
        super().__init__(model, year, position)
        if self.model == "Lacetti":
            self.cost = 138000000 - ((2020 - year) * 5000000)
        elif self.model == "Spark":
            self.cost = 92821000 - ((2020 - year) * 2000000)
        elif self.model == "Malibu":
            self.cost = 311124067 - ((2020 - year) * 3000000)
        else:
            print("Find out the price of the car on the Internet!")

    def malumot(self):
        return "Model: {} Color: {} Year: {} Position: {} Cost: {}".format(
                self.model, self.year, self.position, self.cost
            )
class Full_data(Pass):
    def __init__(self, rusumi, rangi, yili, pozitsiya, carclass):
        super().__init__(rusumi, rangi, yili, pozitsiya)
        if self.model == "Lacetti":
            self.cost = 138000000 - ((2020 - year) * 5000000)
        elif self.model == "Spark":
            self.cost = 92821000 - ((2020 - year) * 2000000)
        elif self.model == "Malibu":
            self.cost = 311124067 - ((2020 - year) * 3000000)
        else:
            print("Find out the price of the car on the Internet!")
        self.classes = carclass
    def data(self):
        return "Model: {} Color: {} Year: {} Position: {} Cost: {}".format(
                self.model, self.year, self.position, self.cost
            )

car_1 = Car("Lacetti", 2018, "3")

print(car_1.model)
print(car_1.year)
print(car_1.position)
print(car_1.cost)
a = car_1.data()
print(a)

你能帮我吗? 它说:“init() 缺少 1 个必需的位置参数:'position'” 我不知道有什么问题? 你能给出答案吗? 请编辑错误并与代码一起发送! 谢谢!

1 个答案:

答案 0 :(得分:0)

您已经在 Car 类中继承了 Pass 类并调用了它的 init 方法,但是您需要向 Pass 类(模型、颜色、年份、位置)发送 4 个参数,因为您还没有使用默认值定义它们中的任何一个。但是,在您的 Car Class 中,您仅发送 3 个值 (args) super().__init__(model, year, position) 。您忘记发送 color 并且 python 假定您将 Car Class 中的位置参数发送到 Pass Class 作为其颜色参数。尊重

  • Car.model = Pass.model
  • Car.year = Pass.year
  • Car.position = Pass.color
  • 汽车。?? = Pass.position
class Pass:
    def __init__(self, model, color, year, position):
        self.model = model
        self.color = color
        self.year = year
        self.position = position

class Car(Pass):
    def __init__(self, model, year, position):
        super().__init__(model, year, position)