首页 > 编程 > Python > 正文

Python重载操作符

2019-11-11 02:08:24
字体:
来源:转载
供稿:网友
class Pair: def __init__(self, a, b): self.a = a self.b = b def __str__(self): return 'Pair (%d, %d)' % (self.a, self.b) def __add__(self,other): return Pair(self.a + other.a, self.b + other.b)v1 = Pair(2, 10)v2 = Pair(5, -2)PRint v1 + v2 #Pair (7, 8)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表