实验五

1.实验任务1:

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)
a2.withdraw(10000)
a2.withdraw(5000)
a1.info()
a2.info()

 

 2. 实验任务2:自定义模块,导入模块

'''
银行账户
数据: 持卡人姓名、账号、当前余额
操作:提款、取款、打印账户信息
'''
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__':
    print('模块信息: ', __doc__)
    print('Account类信息: ', Account.__doc__)
    main()

 

 

import account
u1 = account.Account('Tom', '5004516', 10000)
u2 = account.Account('Jerry', '5003217', 10000)
u1.deposit(5000)
u1.info()
u2.withdraw(5000)
u2.info()

 

 

from account import Account
u1 = Account('Tom', '5004516', 10000)
u2 = Account('Jerry', '5003217', 10000)
u1.deposit(5000)
u1.info()
u2.withdraw(5000)
u2.info()

 

 3. 实验任务3:类的继承

class Shape:
    def info(self):
        print('形状基类')
class Rect(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):
    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 * self._r
    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, 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}')

 

 5. 实验任务5:自定义模块及模块导入编程

class StudentDoc:
    def __init__(self,studentnumber,name,major,point):
        self._studentnumber = studentnumber
        self._name = name
        self._major = major
        self._point = point
    def info(self):
        print(f'学生学号:{self._studentnumber}')
        print(f'学生姓名:{self._name}')
        print(f'学生专业:{self._major}')
        print(f'Python课程分数:{self._point}')
    def change_point(self,newpoint):
        self._point = newpoint
        return newpoint
def main():
    a1 = StudentDoc('123', 'Bob', 'python', '90')
    a1.change_point(99)
    a1.info()
if __name__ == '__main__':
    main()

 

from student import StudentDoc
s1 = StudentDoc('Tom', '','Math', '88')
s2 = StudentDoc('Jerry','','English', '99')
s1.change_point(89)
s1.info()
s2.info()

 

posted @ 2021-05-25 17:24  AOIL  阅读(41)  评论(1编辑  收藏  举报