首页 > 其他分享 >实验7

实验7

时间:2023-06-08 21:48:16浏览次数:21  
标签:self amount 实验 ._ print balance def

task1

源代码:

 1 '''
 2 银行账户
 3 数据:持卡人姓名、账户、当前余额
 4 操作:取款、存款、打印账户信息
 5 返回账户余额
 6 '''
 7 
 8 class Account:
 9     '''一个模拟银行账户的简单类'''
10 
11     def __init__(self,name,account_number,initial_amount = 10):
12         '''构造新账户'''
13         self._name = name
14         self._card_no = account_number
15         self._balance = initial_amount
16 
17     def deposit(self,amount):
18         '''存款'''
19         self._balance += amount
20 
21     def withdraw(self,amount):
22         '''取款'''
23         if self._balance < amount:
24             print('余额不足')
25             return
26 
27         self._balance -= amount
28 
29     def info(self):
30         '''打印账户信息'''
31         print('持卡人姓名:',self._name)
32         print('持卡人账号:',self._card_no)
33         print('持卡人账户余额:',self._balance)
34 
35     def get_balance(self):
36         '''返回账户余额'''
37         return self._balance
38 
39 def main():
40     '''创建Account类对象,测试类'''
41     print('测试账户1:'.center(30,'*'))
42     a1 = Account('Bob','5002311',20000)
43     a1.deposit(5000)
44     a1.withdraw(4000)
45     a1.info()
46 
47     print()
48 
49     print('测试账户2:'.center(30,'*'))
50     a2 = Account('Joe','5006692',20000)
51     a2.withdraw(10000)
52     a2.withdraw(5000)
53     a2.info()
54 
55 if __name__ == '__main__':
56     main()

运行截图:

 回答问题:

1,类:定义一个抽象类型的表现形式,其中包含了适用于该类型的一系列操作和运算,但它实际上可以不含任何实际的数据

  对象:类中一个特定的个体,是类的实例

  属性:类中的变量,提供关于类或对象的额外信息的元素

  方法:类中定义的函数

  实例化:用类创建对象的过程

2.类的封装性:将数据和操作打包成一个不可分割的整体;对外只提供专门的接口供使用者访问部分数据和操作。

 

 

 

 

task2

源代码:

1 from shape import Rect,Circle
2 shape_lst = [Rect(5,5,10,5),Circle(),Circle(1,1,10)]
3 
4 for i in shape_lst:
5     i.info()
6     print(f'面积:{i.area():.2f}')
7     print(f'周长:{i.perimeter():.2f}')
8     print()

运行截图:

 类的继承:对于不同的对象可以直接调用类中的方法,不需要重复编码;也可以在原有的类的基础上,扩展,修改或替换,节省了开发时间

多态:对于不同的实例对象。可以进行不同的计算行为即同名的方法有不同的功能,增强了代码的灵活性。

模块:模块是一个单独的python文件,是对代码更高级的封装,能够实现一类型的功能,方便导入使用,可以避免函数名和变量名冲突。

 

 

 

task3

源代码:

1 import math
2 def func(x,m,s):
3     a = (-1/2)*((x-m)/s)**2
4     y = math.exp(a)/(pow(2*math.pi,0.5)*s)
5     print(f'x = {x},f = {y:.8f}')
6 
7 x = [1,3,5,7,9]
8 for i in x:
9     func(i,0,2)

运行截图:

 

标签:self,amount,实验,._,print,balance,def
From: https://www.cnblogs.com/ljxljx317/p/17467600.html

相关文章

  • 实验7 面向对象编程与内置模块
    实验任务1:模拟银行账户,理解类的封装特性'''银行账户数据:持卡人姓名、账号、当前余额操作:取款、存款、打印账户信息、返回账户余额'''classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'&......
  • 实验七
    实验七实验任务4实验代码#include<stdio.h>intmain(){FILE*file;charfilename[100];charch;intcount=0;file=fopen("data4.txt","r");if(file==NULL){printf("无法打开文件\n");r......
  • 实验7
    #include<stdio.h>#include<stdlib.h>intmain(){FILE*fp1,*fp2,*fp3;charch;fp1=fopen("data4.txt","r");fp2=fp3=fp1;while((ch=getc(fp2))!=EOF){if(ch!=''&&ch!='\......
  • 实验7
     task4.c1#include<stdio.h>2#include<stdlib.h>3#include<string.h>4#defineN1005intmain()6{7chars;8inti=0;9FILE*fp;1011fp=fopen("data4.txt","r");1213......
  • 五月二十五日实验
    实验一略实验二略实验三略实验四#include<stdio.h>#include<string.h>#defineN100typedefstruct{charnum[10];//学号ints1;//期末成绩ints2;//平时成绩doublesum;//总评charlevel[10];//等级}STU;intfun(STUa[],intn,STUh[]);//......
  • 实验6
    task1_1源代码:1fromturtleimport*23defmove(x,y):4'''画笔移动到坐标(x,y)处'''5penup()6goto(x,y)7pendown()89defdraw(n,size=100):10'''绘制边长为size的正n边形'''......
  • 实验七
    任务三#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intchar_count=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("Couldnotopenfiledata4.txt.\n&q......
  • 实验7
    任务4源代码#include<stdio.h>#include<string.h>intmain(){FILE*fp;inti,num=0;chars[7][80];fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfp\n");......
  • 实验6 turtle绘图与python库应用编程体验
    task1-1fromturtleimport*defmove(x,y):penup()goto(x,y)pendown()defdraw(n,size=100):foriinrange(n):fd(size)left(360/n)defmain():pensize(2)pencolor('red')move(-200,0)draw......
  • 实验七
    实验任务4程序代码:#include<stdio.h>#include<string.h>#include<stdlib.h>intmain(){FILE*fp;charstr[100];inti,k=0,flag;while((fp=fopen("data4.txt","r"))==NULL)printf("failtoopen&quo......