Python によるオブジェクト指向プログラミング
Alex Yarosh
Content Quality Analyst @ DataCamp

# Withdraw amount from each of accounts in list_of_accounts def batch_withdraw(list_of_accounts, amount): for acct in list_of_accounts: acct.withdraw(amount)b, c, s = BankAccount(1000), CheckingAccount(2000), SavingsAccount(3000) batch_withdraw([b,c,s]) # <-- Will use BankAccount.withdraw(), # then CheckingAccount.withdraw(), # then SavingsAccount.withdraw()
batch_withdraw() は、どの withdraw() を呼ぶか判断のためにオブジェクトを確認する必要がない
基底クラスは、プログラムの性質を変えずに任意のサブクラスと置換可能であるべき
BankAccount が動く場所では、CheckingAccount も動くべき

基底クラスは、プログラムの性質を変えずに任意のサブクラスと置換可能であるべき
→ 構文の非互換
BankAccount.withdraw() は引数1つ、CheckingAccount.withdraw() は2つを要求
→ サブクラスが入力条件を強化
BankAccount.withdraw() は任意額を受け付けるが、CheckingAccount.withdraw() は金額に制限を仮定
→ サブクラスが出力条件を弱める
BankAccount.withdraw() は残高を正に保つかエラー、CheckingAccount.withdraw() は残高を負にし得る
→ サブクラスのメソッドで追加の属性を変更
→ サブクラスのメソッドで例外を追加して送出
$$\text{\textbf{\Huge{LSPなし=継承なし}}}$$
Python によるオブジェクト指向プログラミング