实验7

实验任务1

实验源码

 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     def deposit(self, amount):
 7         self._balance += amount
 8     def withdraw(self, amount):
 9         if self._balance < amount:
10             print('余额不足')
11             return
12         self._balance -= amount
13     def info(self):
14         print('持卡人姓名:', self._name)
15         print('持卡人账号:', self._card_no)
16         print('持卡人账户余额:', self._balance)
17     def get_balance(self):
18         return self._balance
19 def main():
20     print('测试账户1:'.center(30, '*'))
21     a1 = Account('Bob', '5002311', 20000)
22     a1.deposit(5000)
23     a1.withdraw(4000)
24     a1.info()
25     print()
26     print('测试账户2:'.center(30, '*'))
27     a2 = Account('Joe', '5006692', 20000)
28     a2.withdraw(10000)
29     a2.withdraw(5000)
30     a2.info()
31 if __name__ == '__main__':
32     main()

运行测试截图

总结:类是用来描述具有相同的属性和方法的对象的集合。

对象:类中一个特定的对象,具体的

属性:类的变量提供关于类和对象额外的信息

方法:类中定义函数

类的封装性:是指把数据、方法等封装起来。

实验任务二

实验源码

 1 class Shape:
 2     def info(self):
 3         pass
 4     def area(self):
 5         pass
 6     def perimeter(self):
 7         pass
 8 class Rect(Shape):
 9     def __init__(self, x = 0, y = 0, length = 2, width = 1):
10         self._x = x
11         self._y = y
12         self._width = width
13         self._length = length
14     def info(self):
15         print(f'矩形左上角顶点坐标: ({self._x}, {self._y})')
16         print(f'矩形长: {self._length}')
17         print(f'矩形宽: {self._width}')
18     def area(self):
19         return self._length * self._width
20     def perimeter(self):
21         return (self._length + self._width) * 2
22 class Circle(Shape):
23     def __init__(self, x = 0, y = 0, radius = 1):
24         self._x = x
25         self._y = y
26         self._r = radius
27     def info(self):
28         print(f'圆心: ({self._x}, {self._y})')
29         print(f'半径: {self._r}')
30     def area(self):
31         return 3.14 * self._r * self._r
32     def perimeter(self):
33         return 2 * 3.14 * self._r
34 class Triangle(Shape):
35     def __init__(self, a = 1, b = 1, c = 1):
36         self._a, self._b, self._c = a, b, c
37     def info(self):
38         print(f'三角形三边长: ({self._a}, {self._b}, {self._c})')
39     def area(self):
40         s = (self._a + self._b + self._c) / 2
41         ans = (s*(s - self._a)*(s - self._b)*(s - self._c)) ** 0.5
42         return ans
43     def perimeter(self):
44         return (self._a + self._b + self._c)
45 
46 def main():
47     print('测试1:'.center(40, '*'))
48     shapes_lst1 = [Circle(), Rect(), Triangle()]
49     for t in shapes_lst1:
50         t.info()
51         print(f'面积: {t.area():.2f}')
52         print(f'周长: {t.perimeter():.2f}')
53         print()
54         print('测试2:'.center(40, '*'))
55         shapes_lst2 = [Circle(x = 2, y = 2, radius = 10),
56         Rect(x = 50, y = 50, length = 10, width = 5),
57         Triangle(a = 3, b = 4, c = 5)]
58         for t in shapes_lst2:
59             t.info()
60             print(f'面积: {t.area():.2f}')
61             print(f'周长: {t.perimeter():.2f}')
62             print()
63 if __name__ == '__main__':
64     main()

运行测试截图

类的继承:不同对象可以直接调用方法跟属性

多太特征:同一个函数,实现不同的功能

模块:每个模块都是一个python文件,能够实现一类的导入,使用。

实验任务3

实验源码

1 import math
2 from math import *
3 def func(x):
4     m = 0
5     s = 2
6     fx = 1/((2*math.pi)**0.5*s)*math.exp(-0.5*((x-m)/s)**2)
7     return fx
8 for i in range(1,11,2):
9     print(f'x={i},f={func(i):.8f}')

运行测试截图

 

posted @ 2023-06-12 21:29  永珠拉珍  阅读(17)  评论(0)    收藏  举报