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)
这就是定义了一个类,Comp类,这是类的名字。里面有几个方法,只有init那个方法有具体实现,剩下的几个方法,定义了,但是没写具体实现,需要编程人员填上已经的代码,实现自己想要的功能。