這就是我的代碼。沒(méi)有輸出顯示,也沒(méi)有顯示錯(cuò)
class Programer(object):
? ?def __init__(self,name,age):
? ? ? ?self.name=name
? ? ? ?if isinstance(age,int):
? ? ? ? ? ?self.age=age
? ? ? ?else:
? ? ? ? ? ?raise Exception('age must be int')
? ?def __eq__(self, other):
? ? ? ?if isinstance(other,Programer):
? ? ? ? ? ?if self.age==other.age:
? ? ? ? ? ? ? ?return ?True
? ? ? ? ? ?else:return False
? ? ? ?else:
? ? ? ? ? ?raise Exception('The type of object must be Programer')
? ?def __add__(self, other):
? ? ? ?if ?isinstance(other,Programer):
? ? ? ? ? ?return ?self.age+other.age
? ? ? ?else:
? ? ? ? ? ?raise Exception('The type of age must bu int')
if __name__=='main':
? ?p1=Programer('Albert',25)
? ?p2=Programer('bill',i0)
? ?print(p1==p2)
? ?print('.....')
? ?print(p1+p2)
2018-02-28
不輸出原因如下:
if __name__=='main':? ? >>?if __name__=='__main__':
p2=Programer('bill',i0)? >>?p2=Programer('bill',10)