class Programer(object):
def __init__(self, name, age):
#super(Programer, self).__init__()
#self.arg = arg
self.name = name
if isinstance(age,int):
self.age = age
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 object must be Programer')
if __name__=='__main__':
p1 = Programer('Yue', 20)
p2 = Programer('Wang', 24)
print (p1 == p2)
print (P1 + p2)
结果:
False print (P1 + p2)
NameError: name 'P1' is not defined
相关分类