于熙顿 发表于 2021-8-8 18:07:50

新手求助

class rectangle:
    def __init__(self, length, width):
      self.length = length
      self.width = width
    def s(self):
      return length * width
class square(rectangle):
    def __init__(self, length):
      self.length = length
      self.width = length
a = square(5, 5)
print(a.s)
这段代码有什么问题呢?

南归 发表于 2021-8-8 18:13:51

class rectangle:
    def __init__(self, length, width):
      self.length = length
      self.width = width

    def s(self):
      return self.length * self.width#少了两个self


class square(rectangle):
    def __init__(self, length, width): #少了width参数
      self.length = length
      self.width = length


a = square(5, 5)
print(a.s())   # 方法调用少了括号

冬雪雪冬 发表于 2021-8-8 18:14:06

修改一下:
class rectangle:
    def __init__(self, length, width):
      self.length = length
      self.width = width
    def s(self):
      return self.length * self.width
class square(rectangle):
    def __init__(self, length):
      self.length = length
      self.width = length
a = square(5)
print(a.s())
页: [1]
查看完整版本: 新手求助