标签:情况 参数 操作符 row start http hid 识别 __add__
1 class Time(object): 2 def __init__(self,hour=0,minute=0,second=0): 3 self.hour = hour 4 self.minute = minute 5 self.hour = hour 6 ... 7 def __add__(self,other): 8 ‘‘‘以下仅作示意用,未考虑进位‘‘‘ 9 hour = self.hour + other.hour 10 minute = self.minute + other.hour 11 second = self.second + other.second 12 print(‘%.2d:%.2d:%.2d‘ %(hour,minute,second)) 13 14 start = Time(11,20) 15 end = Time(1,25,20) 16 ‘‘‘当运行+运算符会自动调用Time类中的__add__方法‘‘‘ 17 start + end
1 class Time(object): 2 def __init__(self,hour=0,minute=0,second=0): 3 self.hour = hour 4 self.minute = minute 5 self.second = second 6 ... 7 def __add__(self,other): 8 ‘‘‘内置函数isinstance判断数据类型‘‘‘ 9 if isinstance(other,Time): 10 ‘‘‘类中调用类的函数不需要self参数‘‘‘ 11 self.add_time(other) 12 if isinstance(other, int): 13 self.add_interval(other) 14 def add_time(self,other): 15 ‘‘‘以下仅作示意用,未考虑进位‘‘‘ 16 hour = self.hour + other.hour 17 minute = self.minute + other.minute 18 second = self.second + other.second 19 print(‘%.2d:%.2d:%.2d‘ %(hour,minute,second)) 20 def add_interval(self,other): 21 ‘‘‘以下仅作示意用,未考虑进位‘‘‘ 22 hour = self.hour 23 minute = self.minute 24 second = self.second + other 25 print(‘%.2d:%.2d:%.2d‘ %(hour,minute,second)) 26 27 start = Time(11,20) 28 end = Time(1,25,20) 29 ‘‘‘当运行+运算符会自动调用Time类中的__add__方法‘‘‘ 30 start + end 31 >>>12:45:20 32 end1 = 20 33 ‘‘‘当运行+运算符会自动调用Time类中的__add__方法‘‘‘ 34 start + end1 35 >>>11:20:20
标签:情况 参数 操作符 row start http hid 识别 __add__
原文地址:https://www.cnblogs.com/i-orange/p/9512025.html