老师,应该怎么写代码,才能在重写__add__方法后,实现多个相加,比如注释中写的a+b+c+d,不限制数量
class Person:
def __init__(self,name,salary):
self.name = name
self.salary = salary
def __add__(self, other):
if isinstance(other,Person):
return "{0}和{1}的薪水和为{2}".format(self.name,other.name,self.salary+other.salary)
else:
return "不是同类,不能相加"
def __mul__(self, other):
if isinstance(other,int):
return self.salary*other
a = Person("高淇",10000)
b = Person("高希希",50000)
c = Person("高小齐",1234)
d = Person("高小八",99)
print(a+b)
print(a.salary.__add__(b.salary).__add__(c.salary))
#如何实现多个相加,比如print(a+b+c),print(a+b+c+d),(不限制相加数量)
print(a*13)