python中的OOP,以长方体类为例

python中的OOP,以长方体类为例,python,class,inheritance,Python,Class,Inheritance,我创建了一个类来计算python中矩形的面积和周长 我想扩展这个类,计算长方体的面积和体积 我的尝试如下 class Rectangle (): def __init__(self, L, W): self.length = L self.width = W def rect_perimeter(self): return 2 * (self.length + self.width) def rect_are

我创建了一个类来计算python中矩形的面积和周长

我想扩展这个类,计算长方体的面积和体积

我的尝试如下

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

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

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


class RectangularCuboid (Rectangle):  
    def __init__(self, H):  
        self.height = H
        Rectangle.__init__ (self, L, W)  

    def rect_area(self):  
        return 2 * (self.length * self.width + self.width * self.height + self.height * self.lenght)

    def rect_volume(self):  
        return self.length * self.width * self.height

RC = RectangularCuboid(30, 20, 10)
R = Rectangular(30,20)
RC.rect_area()
RC.rect_volume()
print('Rectangle Cuboid Area:', RC.rect_area(), ', Rectangle Cuboid Volume:', RC.rect_volume())
但是我得到了以下错误

Traceback (most recent call last):
  File "C:/Users/User/.PyCharmCE2019.3/config/scratches/scratch.py", line 24, in <module>
    RC = RectangularCuboid(30, 20, 10)
TypeError: __init__() takes 2 positional arguments but 4 were given

Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“C:/Users/User/.PyCharmCE2019.3/config/scratches/scratch.py”,第24行,在
RC=矩形长方体(30,20,10)
TypeError:\uuuu init\uuuuuu()接受2个位置参数,但提供了4个
进程已完成,退出代码为1
我错过了什么

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

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

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


class RectangularCuboid (Rectangle):  
    def __init__(self, H,L,W):  
        self.height = H
        self.length = L
        self.width = W

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

    def rect_volume(self):  
        return self.length * self.width * self.height


RC = RectangularCuboid(30,20, 10)

RC.rect_area()
RC.rect_volume()
print('Rectangle Cuboid Area:', RC.rect_area(), ', Rectangle Cuboid Volume:', RC.rect_volume())
这应该行得通
RectangleArcuboid继承自Rectangle,但init被重写,因此需要根据需要对其进行调整

只需将矩形长方体的签名从
def\uu init\uuuuuuuuuuuuu(self,H)
更改为
def\uu init\uuuuuuuuu(self,H,L,W)
@Robin Zigmond我这样做了,但随后得到了以下错误
回溯(最近一次调用):文件“C:/Users/User/.PyCharmCE2019.3/config/scratch/scratch.py”,第25行,在RC.rect_区域()文件“C:/Users/User/.PyCharmCE2019.3/config/scratches/scratch.py”的第19行中,rect_区域返回2*(self.length*self.width+self.width*self.height+self.height*self.lenght)AttributeError:“RectangularCuboid”对象没有结束的属性“Length”过程,退出代码为1
RectangularCuboid
继承自
矩形
,这只需要两个参数。调用
rectanglearcuboid(20,10,5)
@betoyla时,您正在向
Rectangle
传递三个参数,请使用新的尝试和相应的新错误更新您的问题。不要使用注释。除了你的技术问题,你的代码暗示长方体是矩形,这从数学/几何的角度来看是毫无意义的。你的答案更适合作为注释。请看一分钟,我的错,已经修好了now@Capie,这不是一个要求,但如果你遵守,你会让这里的人更快乐。我知道pep8的合规性,我每天都使用它,但我在我的手机上发布。。。。在这么小的设备上很难做到:)