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)