Série: Angelia
java
18 lignes
· Mis à jour 2026-09-27
OverdraftSavingsAccount.java
Angelia/Java/Week_6_7/code/03_inheritance_lsp/OverdraftSavingsAccount.java
/**
* 可透支的帳戶:withdraw 永遠成功,餘額可能變成負數。
*
* ★ 這個類別「違反 LSP」:
* SavingsAccount 的契約說「餘額 >= 0」且「餘額不足時回傳 false」,
* 但這裡打破了不變量(餘額可負)也違反了後置條件(永遠 true)。
* 任何以 SavingsAccount 為前提寫的程式,拿到它就可能出錯——即使編譯器完全不會抱怨。
*
* 教材保留它,是為了讓你親眼看到 LSP 被違反的後果(見 Main.java)。
*/
public class OverdraftSavingsAccount extends SavingsAccount {
@Override
public boolean withdraw(int amount) {
this.balance = this.balance - amount; // 不檢查餘額
return true; // 永遠宣稱成功
}
}
Articles liés
Angelia
haskell
EN
Mis à jour 2026-02-03
Adding.hs
Adding.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Adding.hs).
Lire l'article →
Angelia
haskell
EN
Mis à jour 2026-02-03
Area.hs
Area.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Area.hs).
Lire l'article →
Angelia
haskell
EN
Mis à jour 2026-02-03
Chess.hs
Chess.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Chess.hs).
Lire l'article →
Angelia
haskell
EN
Mis à jour 2026-02-03
ChessGraphics.hs
ChessGraphics.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/ChessGraphics.hs).
Lire l'article →
Angelia
haskell
EN
Mis à jour 2026-02-03
Examples.hs
Examples.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Examples.hs).
Lire l'article →
Angelia
haskell
EN
Mis à jour 2026-02-03
Fibonacci.hs
Fibonacci.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Fibonacci.hs).
Lire l'article →