宣告一個bank類別
在main method宣告兩個Rule物件
分別是warrior,wizard
將bank帶入warrior,wizard建構子
這樣warrior wizard儲存同一物件
(應用角色扮演的走地圖與判斷腳色走地圖的位置)
class Bank{
private int count=0;
public void add(){
count++;
}
public void show(){
System.out.println(count);
}
}
class Rule{
private Bank bank ;
Rule(Bank b){
bank=b;
}
public void access(){
bank.add();
} }
public class accessObj {
public static void main(String[] args) {
Bank bank = new Bank();
Rule warrior = new Rule(bank);
Rule wizard= new Rule(bank);
warrior.access();
wizard.access();
bank.show();//印出count=2
}
}
沒有留言:
張貼留言