首页 > 解决方案 > 为什么这个参数在这个 super().__init__() 方法中出现了两次?

问题描述

为什么在 Square 类中super().__init__(length, length)有两个长度参数。

这段代码来自我正在阅读的一篇文章,试图理解 Python 中的 super() 函数。我非常感谢一些建议。提前致谢。

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * self.length + 2 * self.width

# Here we declare that the Square class inherits from the Rectangle class
class Square(Rectangle):
    def __init__(self, length):
        # This is the line that I don't understand.
        super().__init__(length, length)

标签: pythoninheritanceinitsuper

解决方案


推荐阅读