當前位置:編程學習大全網 - 編程軟體 - python這道題啥意思?怎麽做?

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)

  • 上一篇:西門子系統銑圓弧r角怎麽編程
  • 下一篇:上海市松江區有沒有數控廠子
  • copyright 2024編程學習大全網