python - 我做错了什么缺少1个必需的位置参数:'self'
问题描述
class Area(object):
def __init__(self, base, height):
self.base = base
self.height = height
def calculation(self):
return(self.base * self.height)
area = Area(15, 2)
print(Area.calculation())
解决方案
Area
in your是大写的print()
,而你的变量area
不是。这意味着您正在调用区域对象而不是变量。
这应该工作:
class Area(object):
def __init__(self, base, height):
self.base = base
self.height = height
def calculation(self):
return(self.base * self.height)
area = Area(15, 2)
print(area.calculation())
推荐阅读
- c# - 有人可以解释'int sum = 0;'的目的吗 在这段代码中?
- visual-studio - Visual Studio 在空行上添加了空格
- vtk - vtk:为什么来自 vtkPropPicker 和 vtkCoordinate 的世界点不同
- cookiecutter-django - 使用 django cookie 切割器,我可以使用备份命令仅备份和恢复特定表吗?
- javascript - Javascript:使用 catch 块但不处理错误
- javascript - 如何将相同的功能应用于多个div
- php - 如何使用提取的 X.509 数据验证 PDF 并获取信息?
- matlab - 日期时间小时和分钟的平均值/平均值 ITSELF - 不对应的嵌入数据
- python - Pyspark 相当于 pandas 的所有功能
- java - Spring Boot - Raspberry PI - RestTemplate 连接超时