python实现复数加法

如题所述

第1个回答  2012-12-27
class Complex:
def __init__(r, v):
self.r = r
self.v = v
def __add__(self, another):
return Complex(self.r + another.r, self.v + another.v)
def __radd__(self, another):
self.r += another.r
self.v += another.v
return self

...本回答被提问者采纳
第2个回答  2012-12-26
complex是python的builtin type。
你只要直接
x=1+2j
y=2+3j
print x+y
就会有
(3+5j)追问

题目要求编一个类实现

追答

那可能是因为出题的人没用过python吧……

相似回答