Python类定义案例 --面向对象

版权声明:本文博主原创,转载请注明出处,感谢您的合作! https://blog.csdn.net/Thanlon/article/details/89086221

本文转载自:http://www.thanlon.cn/art/desc/13/1

CreditCard类的定义:
class CreditCard:
    def __init__(self,customer,bank,acnt,limit):
        self._customer = customer
        self._bank = bank
        self._acnt = acnt
        self._limit = limit
        self._balance = 0
    def get_customer(self):
        return self._customer
    def get_bank(self):
        return self._bank
    def get_acnt(self):
        return self._acnt
    def get_limit(self):
        return self._limit
    def get_balance(self):
        return self._balance
##    receivales
    def charge(self,price):
        if price + self._balance>self._limit:
            return False
        else:
            self._balance+=price
            return True
##    make payment
    def make_payment(self,amount):
        self._balance-=amount
    
##test Class CreditCard        
if __name__=="__main__":
    wallet = []
    wallet.append(CreditCard('Thanlon','California Finance',
                             '5391 0575 9897 5309',8000))
    wallet.append(CreditCard('Thanlon','California Finance',
                             '3891 0334 6687 5909',9000))
    wallet.append(CreditCard('Thanlon','California Finance',
                             '3391 0344 8387 7609',5000))
##    print(wallet)
    for val in range(1,30):
        wallet[0].charge(val)
        wallet[1].charge(2*val)
        wallet[2].charge(3*val)
    print("balance=",wallet[0].get_balance())
    print("balance=",wallet[1].get_balance())
    print("balance=",wallet[2].get_balance())

    for c in range(3):
        print('Customer=',wallet[c].get_customer())
        print('Bank=',wallet[c].get_bank())
        print('Account=',wallet[c].get_acnt())
        print('Limit=',wallet[c].get_limit())
        print('Balance=',wallet[c].get_balance())
        while wallet[c].get_balance()>100:
            wallet[c].make_payment(100)
            print('New balance = ',wallet[c].get_balance())
        print()
测试结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Thanlon/article/details/89086221