class A: def __init__(self,name,age): self.name = name self.age = age def __add__(self, other): return self.name,other.age obj1 = A("alex",11) obj2 = A("micd",12) r = obj1 + obj2 #两个对象相加是 print(r) class B: def __init__(self,name,age): #查看有哪些对象,以及以字典的形式输出 self.name = name self.age = age self.jimao = 121 obj = B("alex",12) print(obj.__dict__) class C: def __init__(self,name,age): self.name = name self.age = age def __getitem__(self, item): return item+1 def __setitem__(self, key, value): print(key,value) def __delitem__(self, key): print(key) obj = C("alex",12) print(obj.name) print(obj.age) #对象通过索引取值 w = obj[8] #会执行__getitem__方法,8当参数传递给item print(w) obj[2]=7 #会执行__setitem__方法,2传递给key,7传递给value del obj[6] #会执行delitem方法