thisを渡す
非staticからstaticメソッドの呼び出しには自身のインスタンスを渡してあげないといけない
それを”this”を使って渡す
下記で説明すると
呼びだし元
withdrawは非staticメソッド
transfarはstaticメソッド
呼び出し先
chkMoneyはstaticメソッド
この場合のthisはコード2行目のthis!
つまりそれはwithdrawを呼び出している元のインスタンスのこと
public void withdraw(int money) {
if (chkMoney(this, money)) {
this.balance -= money;
} else {
System.out.println(this.name + "の預金残高が不足しています");
}
}
public static void transfer(BankAccount out, BankAccount in, int money) {
if (chkMoney(out, money)) {
out.setBalance(out.getBalance() - money);
} else {
System.out.println(out.getName() + "の預金残高が足りませんでした");
}
}
public static boolean chkMoney(BankAccount out, int money ) {
return out.getBalance() >= money;
}