实验5
'''
银行账户
数据:持卡人姓名、账号、当前余额
操作:提款、取款、打印账户信息
'''
class Account:
'''一个模拟银行账户的简单类'''
def __init__(self, name, account_number, initial_amount): # 构造新账户
self.name = name
self._card_no = account_number
self._balance = initial_amount
def deposit(self, amount): # 存款
self._balance += amount
def withdraw(self, amount): # 取款
self._balance -= amount
def info(self):
print(f'{self.name},{self._card_no},{self._balance}')
def get_balance(self):
return self._balance
# 测试类
a1 = Account('Bob', '5002311', 20000)
a2 = Account('Joe', '5006692', 20000)
a1.deposit(5000)
a1.withdraw(4000)
a1.info()
'''
银行账户
数据:持卡人姓名、账号、当前余额
操作:提款、取款、打印账户信息
'''
class Account:
'''一个模拟银行账户的简单类'''
def __init__(self, name, account_number, initial_amount): # 构造新账户
self._name = name
self._card_no = account_number
self._balance = initial_amount
def deposit(self, amount): # 存款
self._balance += amount
def withdraw(self, amount): # 取款
self._balance -= amount
def info(self):
print(f'{self._name},{self._card_no},{self._balance}')
def get_balance(self):
return self._balance
def main():
a1 = Account('Bob', '5002311', 20000)
a2 = Account('Joe', '5006692', 20000)
a1.deposit(5000)
a1.withdraw(4000)
a2.withdraw(10000)
a2.withdraw(5000)
a1.info()
a2.info()
if __name__=='__main__':#如果模块名是main就执行
print('模块信息:',__doc__)
print('Account类信息:',Account.__doc__)
import account
u1=account.Account('Tom','5004516',10000)
u2=account.Account('Jerrry','5003217',10000)
u1.deposit(5000)
u1.info()
u2.withdraw(5000)
u2.info()
from account import Account
u1=Account('Tom','5004516',10000)
u2=Account('Jerrry','5003217',10000)
u1.deposit(5000)
u1.info()
u2.withdraw(5000)
u2.info()
'''
这是一个图形类模块
包括:
基类:Shape
子类:Rect,Circle
'''
class Shape:
'''形状基类'''
def info(self):
print('形状基类')
class Rect(Shape):
'''
矩阵类,继承自Shape
数据(属性):矩阵左上角的坐标、宽度、高
操作(方法):打印矩阵信息,计算面积,周长
'''
def __init__(self,x,y,width,length):
self._x=x
self._y=y
self._width=width
self._length=length
def info(self):
print(f'矩阵左上角顶点坐标:{self._x},{self._y}')
print(f'矩阵宽:{self._width}')
print(f'矩阵长:{self._length}')
def area(self):
return self._width*self._length
def perimeter(self):
return (self._width+self._length) * 2
class Circle(Shape):
'''
圆形类,继承自Shape
数据(属性):圆心坐标、半径
操作(方法):打印圆信息,计算面积,周长
'''
def __init__(self,x,y,radius):
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 **2)
def perimeter(self):
return 2 * 3.14 * self._r
#测试类
def main():
r1=Rect(1,1,5,9)
r1.info()
print(f'矩形面积:{r1.area()}')
print(f'矩形周长:{r1.perimeter}')
c1=Circle(2,3,10)
c1.info()
print(f'圆面积:{c1.area():.2f}')
print(f'圆周长:{c1.perimeter():.2f}')
if __name__ =='__main__':
main()
from shape import Rect as rect
from shape import Circle as circle
r1=rect(3,3,5,2)
r1.info()
print(f'矩形面积:',r1.area())
print(f'矩形周长:',r1.perimeter())
c1=circle(5,5,1)
c1.info()
print(f'圆面积:c1.area():.2f ')
print(f'圆周长:c1.perimeter():.2f ')