如何使用回调机制? [英] How to use callback mechanism?
问题描述
我必须实施信用卡申请,我必须只处理一个信用卡帐户。像 credit()
, debit()
, pinChange()$ c $等操作c>。
I have to implement a credit card application in which i have to handle only one Credit card account. Operations like credit()
, debit()
, pinChange()
.
但问题是我必须使用JAVA CALLBACK机制在两种情况下通知用户:
But the problem for me is I have to use "JAVA CALLBACK" mechanism to notify user in two cases:
- 更换引脚
- 当余额低于5000时。
如何对这些通知使用回调?
How do I use callbacks for these notifications?
这里更多地关注CALLBACKS的使用..
The use of CALLBACKS is more of focus here..
public interface Callback {
public void onPinChange();
public void onLowBalance();
}
import java.util.Scanner;
public class CreditCard implements Callback{
Callback callback;
int pin;
float balance;
public CreditCard() {
callback = this;
this.pin = 1234; // default pin
this.balance = 10000f; // opening balance
}
public void creditBalance(float amount) {
this.balance = this.balance + amount;
}
public void debitBalance(float amount) {
if (balance <= amount) {
System.out.println("Not enough balance to debit");
} else {
balance = balance - amount;
}
if (balance < 5000) {
callback.onLowBalance();
}
}
public void changePin(int newPin) {
System.out.println("Enter the current pin");
Scanner scanner = new Scanner(System.in);
int existingPin = scanner.nextInt();
if (existingPin != pin) {
System.out.println("Wrong pin!");
} else {
pin = newPin;
callback.onPinChange();
}
scanner.close();
}
@Override
public void onPinChange() {
System.out.println("Pin changed");
}
@Override
public void onLowBalance() {
System.out.println("low balance");
}
public static void main(String[] args) {
CreditCard card = new CreditCard();
card.changePin(3333);
card.debitBalance(5200);
}
}
推荐答案
更改Pin和获得低的余额是CreditCard的行为,因此CreditCard对象是事件生产者而不是事件监听器,所以你不想让CreditCard成为一个监听器(CallBack)。
Changing Pin and getting the balance low is the behavior of CreditCard, so CreditCard object is event producer not event listener so you won't want to make CreditCard a listener (CallBack).
实际上,当发生任何类似pinChange或lowBalance的事件时,您的CreditCard将回拨您想要通知的侦听器的方法。
Actually your CreditCard will be calling back methods of the listener you want to be informed when any event like pinChange or lowBalance occurs.
您的代码应如下所示:
class CreditCard{
int pin, balance;
private Callback callback;
CreditCard(Callback callback){
this.callback=callback;
}
public void pinChange(int pin){
this.pin=pin;
//inform the listener as well
callback.pinChanged();
}
public void withdraw(int amount){
this.balance-=amount;
//inform the the listener
if(balance<1000)callback.lowBalance();
}
}
class MyListener implements Callback{
public void pinChanged(){
//do what is needed when somebody changes pin..
//i.e send sms to the customer
System.out.println("PIN changed..");
}
public void lowBalance(){
//inform the customer about lowbalance.
System.out.println("little money in card..");
}
main(String... args){
CreditCard cc=new CreditCard(new MyListener());
cc.changePin(3306);
}
}
希望这个明白......
Hope this'll clear...
这篇关于如何使用回调机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!