Python 3

您所在的位置:网站首页 python多个对象想加 Python 3

Python 3

2024-02-01 22:15| 来源: 网络整理| 查看: 265

 

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

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3