重载运算符

'''
重载运算符
'''

# 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):#重载的含义就是针对本类型,对+ 重新解释
        self.x+=other.x
        self.y+=other.y



c1=complex(1,2)

c2=complex(3,5)

c1.show()
c2.show()

c1+c2

c1.show()
c2.show()

'''
1 + 2 i
3 + 5 i
4 + 7 i
3 + 5 i
'''
原文地址:https://www.cnblogs.com/liangliangzz/p/11350157.html