zoukankan      html  css  js  c++  java
  • CP学习笔记(6)

    类的定义方法

    class Account:
            def __init__(self, account_holder):
            #必须有`__init__`函数,该函数无需return
                self.balance = 0
                self.holder = account_holder
            def deposit(self, amount):
                self.balance = self.balance + amount
                return self.balance
            def withdraw(self, amount):
                if amount > self.balance:
                    return 'Insufficient funds'
                self.balance = self.balance - amount
                return self.balance
    

    子类的定义

    class CheckingAccount(Account):
            """A bank account that charges for withdrawals."""
            withdraw_charge = 1
            interest = 0.01
            def withdraw(self, amount):
                return Account.withdraw(self, amount + self.withdraw_charge)
    
    
    class SavingsAccount(Account):
            deposit_charge = 2
            def deposit(self, amount):
                return Account.deposit(self, amount - self.deposit_charge) 
    

    多重继承

    python允许多重继承。

    class AsSeenOnTVAccount(CheckingAccount, SavingsAccount):
            def __init__(self, account_holder):
                self.holder = account_holder
                self.balance = 1
    

    多重继承示意图

  • 相关阅读:
    LeetCode#22 Generate Parentheses
    重传
    数学问题——gcdgcl
    数学问题——十进制转N进制
    数据模型
    IEEE
    格与代数系统
    数据字典
    贪心算法
    群论
  • 原文地址:https://www.cnblogs.com/rim99/p/5040817.html
Copyright © 2011-2022 走看看