python这道题啥意思?怎么做?
python这道题是面向对象的用法考查,以复数类的构建为例,结合一点复数知识填入而可,排版和代码如图,注意填入的缩进(选中的代码是题目内容,没选中的是测试代码,效果如下)
class Comp():
def __init__(self,re=1,im=0):
self.__re=re;
self.__im=im;
def __str__(self):
return (
"%f+%fi"%(self.__re,self.__im))
def __lt__ (self, other):
print("复数不能比大小");raise
def __ge__ (self, other):
print("复数不能比大小");raise
def __le__(self, other):
print("复数不能比大小");raise
def __eq__ (self, other):
print("复数不能比大小");raise
def __ne__ (self, other):
print("复数不能比大小");raise
def __add__ (self,other):
return Comp(re=self.__re
+other.__re,im=self.__im
+other.__im)
def __sub__ (self, other):
return Comp(re=self.__re
-other.__re,im=self.__im
-other.__im)