如何添加多个对象的变量

时间:2017-04-01 17:22:22

标签: python-3.x oop methods

我知道使用特殊方法添加相同类的对象,如下面的代码:

class cal(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        return cal(self.x + other.x, self.y + other.y)


def Main():
    obj1 = cal(2, 3)

    obj2 = cal(4, 5)

    obj3 = obj1 + obj2

    print(obj4.x)
    print(obj4.y)


if __name__ == "__main__":
    Main()

这里只添加了两个对象。但如果有两个以上的对象,如obj4 = obj1 + obj2 + obj3,如何添加它们?

0 个答案:

没有答案