class MyClass: def __init__(self, height, width): self.height = height self.width= width # 两个对象 相加 def __add__(self, others): return MyClass(self.height + others.height, self.width + others.width) # 两个对象 相减 def __sub__(self, others): return MyClass(self.height - others.height, self.width - others.width) # def intro(self): print("高为", self.height, " 宽为", self.width) def main(): a = MyClass(height=10, width=5) a.intro() b = MyClass(height=20, width=10) b.intro() c = b - a c.intro() d = a + b d.intro() if __name__ == '__main__': main()
https://www.cnblogs.com/xingchuxin/p/10425667.html