我用c++完成了这项工作,但我正在尝试使用python,遇到了一些困难class mycomplex:
def __init__(self,real=None,imag=None):
self.real=real
self.imag=imag
def read_data(self):
self.real=input("Give the real part :")
self.imag=input("Give the imag part :")
def addition(self,complex):
return mycomplex(self.real+complex.real,self.imag+complex.imag)
def __str__(self):
return ("{0} {1} {2} {3}{4}".format("The complex is : ",self.real,"+",self.imag,"j"))
if __name__=="__main__":
a=mycomplex()
b=mycomplex()
a.read_data()
b.read_data()
print(a)
print(b)
c=a.addition(b)
print(c)
1)首先它不起作用,因为我在init方法中有2个参数,当我试图用a=mycomplex()创建一个实例时,它会给我一个错误当然可以我以某种方式处理这个而不改变init?在
2)为了让我理解,我想用两种方式使用加法,就像我在代码。它我会帮我的想想吧。它与a.addition()和c=a.addition(b)不同。在
3)如果你对实施上述建议有什么更好的建议,请说出来,我想你理解我要给你看的东西。在
谢谢你!在
2747

被折叠的 条评论
为什么被折叠?



