11package com .duwei .designpattern .state2 ;
2- //银行账户:环境类
2+ //银行账户:环境类
33class Account {
4- private AccountState state ; //维持一个对抽象状态对象的引用
5- private String owner ; //开户名
6- private double balance = 0 ; //账户余额
4+ private AccountState state ; //维持一个对抽象状态对象的引用
5+ private String owner ; //开户名
6+ private double balance = 0 ; //账户余额
77
88 public Account (String owner ,double init ) {
99 this .owner = owner ;
1010 this .balance = balance ;
11- this .state = new NormalState (this ); //设置初始状态
12- System .out .println (this .owner + "开户,初始金额为" + init );
11+ this .state = new NormalState (this ); //设置初始状态
12+ System .out .println (this .owner + "开户,初始金额为" + init );
1313 System .out .println ("---------------------------------------------" );
1414 }
1515
@@ -26,23 +26,23 @@ public void setState(AccountState state) {
2626 }
2727
2828 public void deposit (double amount ) {
29- System .out .println (this .owner + "存款" + amount );
30- state .deposit (amount ); //调用状态对象的deposit()方法
31- System .out .println ("现在余额为" + this .balance );
32- System .out .println ("现在帐户状态为" + this .state .getClass ().getName ());
29+ System .out .println (this .owner + "存款" + amount );
30+ state .deposit (amount ); //调用状态对象的deposit()方法
31+ System .out .println ("现在余额为" + this .balance );
32+ System .out .println ("现在帐户状态为" + this .state .getClass ().getName ());
3333 System .out .println ("---------------------------------------------" );
3434 }
3535
3636 public void withdraw (double amount ) {
37- System .out .println (this .owner + "取款" + amount );
38- state .withdraw (amount ); //调用状态对象的withdraw()方法
39- System .out .println ("现在余额为" + this .balance );
40- System .out .println ("现在帐户状态为" + this . state .getClass ().getName ());
37+ System .out .println (this .owner + "取款" + amount );
38+ state .withdraw (amount ); //调用状态对象的withdraw()方法
39+ System .out .println ("现在余额为" + this .balance );
40+ System .out .println ("现在帐户状态为" + this . state .getClass ().getName ());
4141 System .out .println ("---------------------------------------------" );
4242 }
4343
4444 public void computeInterest ()
4545 {
46- state .computeInterest (); //调用状态对象的computeInterest()方法
46+ state .computeInterest (); //调用状态对象的computeInterest()方法
4747 }
4848}
0 commit comments