首页 > 解决方案 > 我做错了什么缺少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())

标签: pythonclassself

解决方案


Areain 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())

推荐阅读