首页 > 其他分享 >实验7

实验7

时间:2023-06-12 21:47:28浏览次数:67  
标签:__ return self 实验 ._ print def

实验任务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}')

运行测试截图

 

标签:__,return,self,实验,._,print,def
From: https://www.cnblogs.com/yongzhulazhen/p/17462771.html

相关文章

  • 实验7 面向对象编程与内置模块
    task1程序源码:1'''2银行账户3数据:持卡人姓名、账号、当前余额4操作:取款、存款、打印账户信息、返回账户余额5'''6classAccount:7'''一个模拟银行账户的简单类'''89def__init__(self,name,account_number,initial_amount=1......
  • 实验7 面向对象编程与内置模块
    task1实验源码:1#12classAccount:3def__init__(self,name,account_number,initial_amount=10):4self._name=name5self._card_no=account_number6self._balance=initial_amount78defdeposit(self,amount):9......
  • 实验7 面向对象编程与内置模块
    实验任务1task1.py程序源码:1'''2银行账户3数据:持卡人姓名、账号、当前余额4操作:取款、存款、打印账户信息、返回账户余额5'''67classAccount:8'''一个模拟银行账户的简单类'''9def__init__(self,name,account_number,initial_a......
  • 实验7
    task1.py实验源码:1classAccount:2#一个模拟账户类3def__init__(self,name,account_number,initial_amount=10):4'''构造新账户'''5self._name=name6self._card_no=account_number7sel......
  • CJJC项目实验环境配置
    CJJC项目实验环境配置 (base)D:\>(base)D:\>condacreate-nwind_2022python==3.7Solvingenvironment:done##PackagePlan##environmentlocation:D:\Anaconda3\envs\wind_2022added/updatedspecs:-python==3.7Thefollowingpackages......
  • 实验7
    实验任务1task1源代码:classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'''构造新账户'''self._name=nameself._card_no=ac......
  • 实验7
    实验任务1:'''银行账户数据:持卡人姓名、账号、当前余额操作:取款、存款、打印账户信息、返回账户余额'''classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'''构造新......
  • 实验七
    实验1task1.py实验源码:1classAccount:2'''一个模拟银行账户的简单类'''3def__init__(self,name,account_number,initial_amount=10):4'''构造新账户'''5self._name=name6......
  • 实验六
    task11fromturtleimport*2defmove(x,y):34penup()5goto(x,y)6pendown()7defdraw(n,size=100):89foriinrange(n):10fd(size)11left(360/n)12defmain():13pensize(2)14pencolor(&......
  • 实验六
    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)d......