如何用Python解决这个继承问题?

如何用Python解决这个继承问题?,python,python-3.x,oop,inheritance,python-3.7,Python,Python 3.x,Oop,Inheritance,Python 3.7,在下面用Python编写的脚本中,我想做的是计算圆形、方形和矩形的面积。其中,我想实现从square.py到rectangle.py的继承。然而,我的继承实现失败了,需要一些帮助来解决这个问题 当我试图计算一个矩形的面积时,我得到了以下记录的错误消息 Traceback (most recent call last): File "script.py", line 28, in <module> print(rectangle.sqArea()) File "/hom

在下面用Python编写的脚本中,我想做的是计算圆形、方形和矩形的面积。其中,我想实现从square.py到rectangle.py的继承。然而,我的继承实现失败了,需要一些帮助来解决这个问题

当我试图计算一个矩形的面积时,我得到了以下记录的错误消息

Traceback (most recent call last):
  File "script.py", line 28, in <module>
    print(rectangle.sqArea())
  File "/home/yosuke/Dropbox/Coding_dir/Python/Originals/OOP/Mathmatics/testdir2/rectangle.py", line 8, in sqArea
    return self.length * self.height
AttributeError: 'Rectangle' object has no attribute 'length'
圆圈.py

class Circle:
    def __init__(self, radius):
        self.radius = radius

    def cirArea(self):
        return self.radius * self.radius * 3.14
square.py

class Square:
    def __init__(self, length):
        self.length = length

    def sqArea(self):
        return self.length * self.length
矩形.py

from square import Square

class Rectangle(Square):
    def __init__(self, height):
        self.height = height

    def sqArea(self):
        return self.length * self.height

您忘记调用超类的
\uuuu init\uuu

class Rectangle(Square):
    def __init__(self, height):
        super().__init__(height)

矩形的构造函数只接收高度,并且只设置其高度属性。它没有长度

在矩形中,类似这样的操作可能会起作用:

def __init__(self, length, height):
    super().__init__(length)
    self.height = height
但是你的课很奇怪。矩形不是正方形,正方形是矩形。因此,如果有什么不同的话,继承应该朝着另一个方向工作


但我想你很快就会遇到问题。事实证明,面向对象编程并不能很好地对这类事情进行建模,事实上,不同形状之间并没有太多共享的东西。

我不确定矩形从正方形继承的概念意义。所有的正方形都是长方形,但不是所有的长方形都是正方形。我认为继承是另一种方式。正方形是一个长度和高度相等的矩形。
def __init__(self, length, height):
    super().__init__(length)
    self.height = height