class CellPhone: # 定义一个手机类的模板,包括品牌(brand)和价格(price)
def __init__(self, brand, price=0.0):
self.brand = brand
self.price = price
def __str__(self):
return str(self.price)
def __add__(self, other): # 魔术函数,self 指实例化对象本身,other 指另一个实例化对象本身.
return CellPhone(brand='', price=(self.price + other.price)) # 将实例化对象的价格求和,得到两个品牌手机价格的和.
c1 = CellPhone('iPhone 7', 7000.0) # 实例化对象1
c2 = CellPhone('MI note 2', 2000.0) # 实例化对象2
c3 = CellPhone('Nokia', 1000.0) # 实例化对象3
print(c1 + c2 + c3)