实验任务1
task1.py
程序源码:
1 ''' 2 银行账户 3 数据:持卡人姓名、账号、当前余额 4 操作:取款、存款、打印账户信息、返回账户余额 5 ''' 6 7 class Account: 8 '''一个模拟银行账户的简单类''' 9 def __init__(self, name, account_number, initial_amount = 10): 10 '''构造新账户''' 11 self._name = name 12 self._card_no = account_number 13 self._balance = initial_amount 14 15 def deposit(self, amount): 16 '''存款''' 17 self._balance += amount 18 19 def withdraw(self, amount): 20 '''取款''' 21 if self._balance < amount: 22 print('余额不足') 23 return 24 self._balance -= amount 25 26 def info(self): 27 '''打印账户信息''' 28 print('持卡人姓名:', self._name) 29 print('持卡人账号:', self._card_no) 30 print('持卡人账户余额:', self._balance) 31 32 def get_balance(self): 33 '''返回账户余额''' 34 return self._balance 35 36 def main(): 37 '''创建Account类对象,测试类''' 38 39 print('测试账户1:'.center(30, '*')) 40 a1 = Account('Bob', '5002311', 20000) 41 a1.deposit(5000) 42 a1.withdraw(4000) 43 a1.info() 44 45 print() 46 47 print('测试账户2:'.center(30, '*')) 48 a2 = Account('Joe', '5006692', 20000) 49 a2.withdraw(10000) 50 a2.withdraw(5000) 51 a2.info() 52 53 if __name__ == '__main__': 54 main()
运行测试截图:
1.类:具有相同属性和方法的对象的集合
属性:在类体内定义的变量
方法:在类内定义的函数
对象:类的实例
2.类的封装性:将众多属性和方法在类里面实现,外部通过接口访问
实验任务2
shape.py
程序源码:
1 ''' 2 shape.py是一个图形类模块 3 包括: 4 基类: 图形类Shape 5 派生类: 矩形类Rect, 圆形类Circle, 三角形类Triangle 6 ''' 7 8 class Shape: 9 '''形状基类''' 10 def info(self): 11 '''打印图形信息''' 12 pass 13 14 def area(self): 15 '''计算面积''' 16 pass 17 18 def perimeter(self): 19 '''计算周长''' 20 pass 21 22 class Rect(Shape): 23 ''' 24 矩形类, 继承自Shape 25 属性:矩形左上角点的坐标、宽、高 26 方法: 打印矩形信息,计算面积、周长 27 ''' 28 def __init__(self, x = 0, y = 0, length = 2, width = 1): 29 '''构造矩形对象,根据矩形左上角顶点坐标(x,y)和长、宽''' 30 self._x = x 31 self._y = y 32 self._width = width 33 self._length = length 34 35 def info(self): 36 print(f'矩形左上角顶点坐标: ({self._x}, {self._y})') 37 print(f'矩形长: {self._length}') 38 print(f'矩形宽: {self._width}') 39 40 def area(self): 41 return self._length * self._width 42 43 def perimeter(self): 44 return (self._length + self._width) * 2 45 46 class Circle(Shape): 47 ''' 48 圆形类,继承自Shape 49 属性:圆心坐标、半径 50 方法: 打印圆信息,计算面积、周长 51 ''' 52 def __init__(self, x = 0, y = 0, radius = 1): 53 '''构造圆形对象,圆心坐标(x,y), 半径r''' 54 self._x = x 55 self._y = y 56 self._r = radius 57 58 def info(self): 59 print(f'圆心: ({self._x}, {self._y})') 60 print(f'半径: {self._r}') 61 62 def area(self): 63 return 3.14 * self._r * self._r 64 65 def perimeter(self): 66 return 2 * 3.14 * self._r 67 68 class Triangle(Shape): 69 ''' 70 三角形类,继承自Shape 71 属性:三边边长 72 方法:打印三角形信息,计算周长、面积 73 ''' 74 def __init__(self, a = 1, b = 1, c = 1): 75 self._a, self._b, self._c = a, b, c 76 77 def info(self): 78 print(f'三角形三边长: ({self._a}, {self._b}, {self._c})') 79 80 def area(self): 81 s = (self._a + self._b + self._c) / 2 82 ans = (s*(s - self._a)*(s - self._b)*(s - self._c)) ** 0.5 83 return ans 84 85 def perimeter(self): 86 return (self._a + self._b + self._c) 87 88 # 测试类 89 def main(): 90 print('测试1:'.center(40, '*')) 91 shapes_lst1 = [Circle(), Rect(), Triangle()] 92 for t in shapes_lst1: 93 t.info() 94 print(f'面积: {t.area():.2f}') 95 print(f'周长: {t.perimeter():.2f}') 96 print() 97 98 print('测试2:'.center(40, '*')) 99 100 shapes_lst2 = [Circle(x=2, y=2, radius=10), 101 Rect(x=50, y=50, length=10, width=5), 102 Triangle(a=3, b=4, c=5)] 103 for t in shapes_lst2: 104 t.info() 105 print(f'面积: {t.area():.2f}') 106 print(f'周长: {t.perimeter():.2f}') 107 print() 108 109 if __name__ == '__main__': 110 main()
运行测试截图:
task2.py
程序源码:
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()
运行测试截图:
实验任务3
task3.py
程序源码:
1 import math 2 def func(x): 3 a=1/(math.sqrt(2*math.pi)*2) 4 b=-0.5*((x/2)**2) 5 c=math.exp(b) 6 return a*c 7 8 print(f'x = 1,f = {func(1):.8f}') 9 print(f'x = 3,f = {func(3):.8f}') 10 print(f'x = 5,f = {func(5):.8f}') 11 print(f'x = 7,f = {func(7):.8f}') 12 print(f'x = 9,f = {func(9):.8f}')
运行测试截图:
标签:__,info,内置,面向对象编程,self,模块,._,print,def From: https://www.cnblogs.com/cl040302/p/17476066.html