2

我已经搞乱了参数和一般的类,但我仍然无法让它工作。即使我删除self.v3了我需要的,我也会得到None第二个输出......

class Base(object):
    def __init__(self, v1, v2):
        self.v1 = v1
        self.v2 = v2
    def together(self):
        return self.v1 + self.v2

class Sub1(Base):
    def __init__(self, v1, v2, v3):
        super(Sub1, self).__init__(v1, v2)
        self.v3 = v3
    def together(self):
        super(Sub1, self).together() + self.v3
b1 = Base(1,2)
print b1.together()
s1 = Sub1(3,2,1)
print s1.together()

输出:3, 6

4

1 回答 1

1

您的子类的together方法实际上并不返回该值。将其更改为

return super(Sub1, self).together() + self.v3
于 2012-12-08T00:41:24.610 回答