实验一:复数类complex
class complex: def __init__(self,real_num,imaginary_num): self.__real_num = real_num self.__imaginary_num = imaginary_num def add(self,c2): print(‘结果为:\n实数:‘,self.__real_num+c2.__real_num,‘虚数:‘,self.__imaginary_num+c2.__imaginary_num) def show(self): print(‘实数:‘,self.__real_num,‘虚数:‘,self.__imaginary_num) c1 = complex(4,-1) c2 = complex(2,3) c1.show() c2.show() c1.add(c2)
实验二:抽象基类shape
import math class shape(object): pass class rectangle(shape): def __init__(self,length, width): self.__length = length self.__width = width def getCircumference(self): circumference = 2*(self.__length+self.__width) print(circumference) def getArea(self): area = self.__length*self.__width print(area) class square(rectangle): def __init__(self, side, ): self.__side = side def getCircumference(self): circumference = 4*self.__side print(circumference) def getArea(self): area = self.__side*self.__side print(area) class circle(shape): def __init__(self,diameter): self.__diameter = diameter def getCircumference(self): circumference = math.pi*self.__diameter print(circumference) def getArea(self): area = 0.25*math.pi*self.__diameter print(area) a = rectangle(7,8) a.getCircumference() a.getArea() b = square(4) b.getCircumference() b.getArea() c = circle(5) c.getCircumference() c.getArea()
时间: 2024-10-23 21:35:35