【发布时间】:2023-03-25 05:57:01
【问题描述】:
我是 OOP 的初学者,一直在尝试使用 Python3 自学其中的一些概念。但是,我已经被继承困住了。这是我的源代码:
#! /usr/bin/env python3
class TwoD:
def __init__(self, height, width):
self.h = height
self.w = width
def perimeter(self, height, width):
return 2 * (height + width)
def area(self, height, width):
return height * width
class Cuboid(TwoD):
def __init__(self, height, width, depth):
self.d = depth
def volume(self, height, width, depth):
return height * width * depth
x = Cuboid(3, 4, 5)
print(x.volume())
print(x.perimeter())
print(x.area())
我运行它时遇到的错误如下。读起来好像我需要为卷添加参数,但 x 不是为它提供了所需的变量吗?
Traceback (most recent call last):
File "./Class.py", line 19, in <module>
print(x.volume())
TypeError: volume() missing 3 required positional arguments: 'height', 'width', and 'depth'
所以有人可以让我知道我做错了什么。我敢肯定这很愚蠢。另外,有人可以解释一下我将如何在 Python3 中使用多重继承吗?
提前致谢
【问题讨论】:
-
请将所有标签替换为 4 个空格。
标签: class oop inheritance python-3.x multiple-inheritance