实验任务1
task1.py
class Account: def __init__(self, name, account_number, initial_amount = 10): self._name=name self._card_no=account_number self._balance=initial_amount def deposit(self,amount): self._balance+=amount def withdraw(self,amount): if self._balance<amount: print('余额不足') return self._balance-=amount def info(self): print('持卡人姓名:',self._name) print('持卡人账号:',self._card_no) print('持卡人账户余额:',self._balance) def get_balance(self): return self._balance def main(): print('测试账户1:'.center(30,'*')) a1=Account('Bob','5002311',20000) a1.deposit(5000) a1.withdraw(4000) a1.info() print() print('测试账户2:'.center(30,'*')) a2=Account('Joe','5006692',20000) a2.withdraw(10000) a2.withdraw(5000) a2.info() if __name__=='__main__': main()
实验结论:
类:类是一群具有相同属性和方法的对象的集合,是对象的抽象
对象:是由类创建出来的一个具体的存在
属性:用来描述对象的特征
方法:用来描述对象的行为
实例化:指在面向对象的编程中,用类创建对象的过程
类的封装性:指将类的属性和方法封装起来,不允许外部直接访问对象的内部信息
实验任务2
shape.py
''' shape.py是一个图形类模块 包括: 基类: 图形类Shape 派生类: 矩形类Rect, 圆形类Circle, 三角形类Triangle ''' class Shape: '''形状基类''' def info(self): '''打印图形信息''' pass def area(self): '''计算面积''' pass def perimeter(self): '''计算周长''' pass class Rect(Shape): ''' 矩形类, 继承自Shape 属性:矩形左上角点的坐标、宽、高 方法: 打印矩形信息,计算面积、周长 ''' def __init__(self, x = 0, y = 0, length = 2, width = 1): '''构造矩形对象,根据矩形左上角顶点坐标(x,y)和长、宽''' self._x = x self._y = y self._width = width self._length = length def info(self): print(f'矩形左上角顶点坐标: ({self._x}, {self._y})') print(f'矩形长: {self._length}') print(f'矩形宽: {self._width}') def area(self): return self._length * self._width def perimeter(self): return (self._length + self._width) * 2 class Circle(Shape): ''' 圆形类,继承自Shape 属性:圆心坐标、半径 方法: 打印圆信息,计算面积、周长 ''' def __init__(self, x = 0, y = 0, radius = 1): '''构造圆形对象,圆心坐标(x,y), 半径r''' self._x = x self._y = y self._r = radius def info(self): print(f'圆心: ({self._x}, {self._y})') print(f'半径: {self._r}') def area(self): return 3.14 * self._r * self._r def perimeter(self): return 2 * 3.14 * self._r class Triangle(Shape): ''' 三角形类,继承自Shape 属性:三边边长 方法:打印三角形信息,计算周长、面积 ''' def __init__(self, a = 1, b = 1, c = 1): self._a, self._b, self._c = a, b, c def info(self): print(f'三角形三边长: ({self._a}, {self._b}, {self._c})') def area(self): s = (self._a + self._b + self._c) / 2 ans = (s*(s - self._a)*(s - self._b)*(s - self._c)) ** 0.5 return ans def perimeter(self): return (self._a + self._b + self._c) def main(): print('测试1:'.center(40, '*')) shapes_lst1 = [Circle(), Rect(), Triangle()] for t in shapes_lst1: t.info() print(f'面积: {t.area():.2f}') print(f'周长: {t.perimeter():.2f}') print() print('测试2:'.center(40, '*')) shapes_lst2 = [Circle(x = 2, y = 2, radius = 10), Rect(x = 50, y = 50, length = 10, width = 5), Triangle(a = 3, b = 4, c = 5)] for t in shapes_lst2: t.info() print(f'面积: {t.area():.2f}') print(f'周长: {t.perimeter():.2f}') print() if __name__ == '__main__': main()
task2.py
from shape import Rect, Circle\ shape_lst = [Rect(5, 5, 10, 5), Circle(), Circle(1, 1, 10)] for i in shape_lst: i.info() print(f'面积: {i.area(): .2f}') print(f'周长: {i.perimeter(): .2f}') print()
实验结论:
类的继承:指在一个现有类的基础上创建一个新的类,构建出来的新的类会继承原有的类的属性和方法
类的多态特性:在一个类中,可以定义多个同名的方法,只要确定他们的参数个数和类型不同,就是类的多态性
模块:一个文件就是一个模块,每一个模块在python中都被看做是一个独立的文件
实验任务3
import math from math import * def func(x): m = 0 s = 2 fx = 1/((2*math.pi)**0.5*s)*math.exp(-0.5*((x-m)/s)**2) return fx for i in range(1,11,2): print(f'x={i},f={func(i):.8f}')
标签:__,内置,面向对象编程,self,Shape,模块,._,print,def From: https://www.cnblogs.com/aaaaaaaaaa/p/17478483.html