''' 重载运算符强化--返回值 ''' # print(1+2)#不同的类型有不同的解释 # print('1'+'2') class complex: def __init__(self,x,y): self.x=x self.y=y def show(self): print(self.x,'+',self.y,'i') def __add__(self, other):#重载的含义就是针对本类型,对+ 重新解释 if type(other)==type(self): return complex(self.x+other.x,self.y+other.y)#加法的返回值 elif type(other)==type(10): return complex(self.x + other,self.y + other) c1=complex(1,2) c2=complex(3,5) c1.show() c2.show() c3=c1+c2 c4=c1.__add__(c2)#效果同c3 c5=c1+10 c1.show() c2.show() c3.show() c4.show() c5.show() ''' 1 + 2 i 3 + 5 i 1 + 2 i 3 + 5 i 4 + 7 i 4 + 7 i 11 + 12 i '''