实验7 面向对象编程与内置模块
实验任务一
task1:
源代码:
1 class Account: 2 def __init__(self,name,account_number,initial_amount = 10): 3 self._name = name 4 self._card_no = account_number 5 self._balance = initial_amount 6 7 def deposit(self,amount): 8 self._balance += amount 9 10 def withdraw(self,amount): 11 if self._balance < amount: 12 print('余额不足') 13 return 14 15 self._balance -= amount 16 17 def info(self): 18 print('持卡人姓名:',self._name) 19 print('持卡人账号:',self._card_no) 20 print('持卡人账户余额:',self._balance) 21 22 def get_balance(self): 23 return self._balance 24 25 def main(): 26 print('测试账户1:'.center(30,'*')) 27 a1 = Account('Bob','5002311',20000) 28 a1.deposit(5000) 29 a1.withdraw(4000) 30 a1.info() 31 32 print() 33 34 print('测试账户2:'.center(30,'*')) 35 a2 = Account('Joe','5006692',20000) 36 a2.withdraw(10000) 37 a2.withdraw(5000) 38 a2.info() 39 40 if __name__ == '__main__': 41 main()
运行结果:
总结:1.创建出来的对象叫做类的实例;创建对象的动作叫做实例化;对象的属性叫做实例属性;对象调用的方法叫做实例方法。
类属性是给类对象定义的属性;类属性用来记录与这个类相关的特征;类属性不会用于记录具体对象的特征。
2.封装就是指隐藏对象中一些不希望外部所访问到的属性或方法,即为了保证安全。
实验任务二
task2:
源代码:
1 class Shape(): 2 def info(self): 3 pass 4 5 def area(self): 6 pass 7 8 def perimeter(self): 9 pass 10 11 class Rect(Shape): 12 def __init__(self,x = 0, y = 0, length = 2,width = 1): 13 self._x = x 14 self._y = y 15 self._width = width 16 self._length = length 17 18 def info(self): 19 print(f'矩形左上角顶点坐标:({self._x},{self._y})') 20 print(f'矩形长:{self._length}') 21 print(f'矩形宽:{self._width}') 22 23 def area(self): 24 return self._length * self._width 25 26 def perimeter(self): 27 return (self._length+self._width)*2 28 29 class Circle(Shape): 30 def __init__(self,x = 0,y = 0,radius = 1): 31 self._x = x 32 self._y = y 33 self._r = radius 34 35 def info(self): 36 print(f'圆心:({self._x},{self._y})') 37 print(f'半径:{self._r}') 38 39 def area(self): 40 return 3.14*self._r*self._r 41 42 def perimeter(self): 43 return 2*3.14*self._r 44 45 class Triangle(Shape): 46 def __init__(self,a = 1,b = 1,c = 1): 47 self._a,self._b,self._c = a,b,c 48 49 def info(self): 50 print(f'三角形边长:({self._a},{self._b},{self._c})') 51 52 def area(self): 53 s = (self._a + self._b + self._c)/2 54 ans = (s*((s-self._a)*(s-self._b)*(s-self._c))**0.5) 55 56 return ans 57 58 def perimeter(self): 59 return (self._a + self._b + self._c) 60 61 def main(): 62 print('测试1:'.center(40,'*')) 63 64 shapes_lst1 = [Circle(),Rect(),Triangle()] 65 66 for t in shapes_lst1: 67 t.info() 68 print(f'面积:{t.area():.2f}') 69 print(f'周长:{t.perimeter():.2f}') 70 print() 71 72 print('测试2:'.center(40,'*')) 73 74 shapes_lst2 = [Circle(x = 2, y = 2, radius = 10), 75 Rect(x = 50, y = 50,length=10,width=5), 76 Triangle(a = 3,b = 4, c = 5)] 77 78 for t in shapes_lst2: 79 t.info() 80 print(f'面积:{t.area():.2f}') 81 print(f'周长:{t.perimeter():.2f}') 82 83 if __name__ == '__main__': 84 main()
运行结果:
总结:1.继承即一个类可以派生出新的类,而且新的类能继承基类的成员;多态性是指相同的操作或方法可作用于多种类型的对象,并获得不同的结果,多态性是通过继承来实现的。
2.模块就好比是工具包,要想使用这个工具包中的工具(就好比函数),就需要导入这个模块。
task2-2:
源代码:
1 from Shape import Rect, Circle 2 3 shape_lst = [Rect(5,5,10,5), Circle(), Circle(1,1,10)] 4 5 for i in shape_lst: 6 i.info() 7 print(f'面积:{i.area():.2f}') 8 print(f'周长:{i.perimeter():.2f}') 9 print()
运行结果:
实验任务三
task3:
源代码:
1 import math 2 3 def func(x): 4 m = 0 5 s = 2 6 fx = 1/(pow(2*math.pi,0.5)*s)*math.exp((-1/2)*pow((x-m)/s,2)) 7 return fx 8 9 def main(): 10 x = [i for i in range(1,10,2)] 11 for i in x: 12 print(f'x = {i},f = {func(i):.8f}') 13 14 main()
运行结果:
实验任务四
task4-1:
源代码:
1 from random import choice 2 3 class RandomWalk(): 4 5 def __init__(self,num_points = 5000): 6 7 self.num_points = num_points 8 9 self.x_values = [0] 10 self.y_values = [0] 11 12 def fill_walk(self): 13 14 while len(self.x_values) < self.num_points: 15 x_direction = choice([1,-1]) 16 x_distance = choice([0,1,2,3,4]) 17 x_step = x_direction*x_distance 18 19 y_direction = choice([1,-1]) 20 y_distance = choice([0,1,2,3,4]) 21 y_step = y_direction*y_distance 22 23 if x_step == 0 and y_step == 0: 24 continue 25 26 next_x = self.x_values[-1] + x_step 27 next_y = self.y_values[-1] + y_step 28 29 self.x_values.append(next_x) 30 self.y_values.append(next_y) 31 32 def main(): 33 rw = RandomWalk(5) 34 rw.fill_walk() 35 print(rw.x_values) 36 print(rw.y_values) 37 38 if __name__ == '__main__': 39 main()
运行结果:
task4-2:
源代码:
1 from matplotlib import pyplot as plt 2 from random_walk import RandomWalk 3 from time import sleep 4 5 n = 0 6 while n< 2 : 7 n += 1 8 9 rw = RandomWalk(50000) 10 rw.fill_walk() 11 12 plt.figure(figsize = (10,6), dpi = 128) 13 point_numbers = list(range(rw.num_points)) 14 plt.scatter(rw.x_values,rw.y_values,c = point_numbers,cmap = plt.cm.Blues,edgecolor = 'none',s=1) 15 16 plt.scatter(0,0,c = 'grey',edgecolors = 'none',s = 100) 17 plt.scatter(rw.x_values[-1],rw.y_values[-1],c = 'red',edgecolors = 'none',s = 100) 18 19 plt.axis('off') 20 21 plt.show()
运行结果: