码迷,mamicode.com
首页 > 其他好文 > 详细

实验五

时间:2021-06-02 19:24:45      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:模块   inf   from   imp   err   dep   pos   __init__   sel   

复制代码

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()

复制代码

 

 

复制代码

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()

复制代码

 

 

复制代码

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}‘)

 

class StudentDoc:

    ‘‘‘一个管理学生档案的简单类‘‘‘

    def __init__(self,number,name,major,point):

        self._number=number

        self._name=name

        self._major=major

        self._point=point

    def info(self):

        print(f‘{self._number},{self._name},{self._major},{self._point}‘)

    def get_balance(self):

        print(f‘{self._name}:{self._point}‘)

    def modify(self,point):

        self._point=point

 

def main():

    a1=StudentDoc(‘2020‘,‘LB‘,‘生态‘,‘97‘)

    a2=StudentDoc(‘2019‘,‘AH‘,‘数学‘,‘95‘)

 

    a1.get_balance()

    a2.get_balance()

 

    a1.modify(99)

    a2.modify(100)

 

    a1.info()

    a2.info()

 

 

if __name__ == ‘__main__‘:

    print(‘模块信息: ‘, __doc__)

    print(‘类信息: ‘, StudentDoc.__doc__)

    main()

 

实验五

标签:模块   inf   from   imp   err   dep   pos   __init__   sel   

原文地址:https://www.cnblogs.com/hcy15904645511/p/14833499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!