python学习之运用特殊方法,定制类

class Time60(object):
def __init__(self,hr,min):
self.hr=hr
self.min=min

def __add__(self, other): #__add__ 相当于加:+操作符
return self.__class__(self.hr+other.hr,self.min+other.min)
#self.__class__()等价Time60()

def __str__(self):
return '%d:%d'%(self.hr,self.min)
__repr__=__str__

def __iadd__(self, other): #__iadd__相当于:+=操作符
self.hr+=other.hr
self.min+=other.min
return self

test1=Time60(1,30)
test2=Time60(12,45)
print(test1,test2)
print(test1+test2)
test1+=test2
print(test1)
上一篇:dubbo 常见错误


下一篇:2020/4/2 与柠檬擦肩而过的感觉——柠檬微趣的笔试题目