Java基礎知識(二) 回調函數

public class EventNotifier {
private InterestingEvent ie;

private boolean somethingHappened;

//Delphi中的寫法
//property OnEvent : TOnEvent read FOnEvent write FOnEvent;
public EventNotifier(InterestingEvent event) {
    
this.ie = event;
    somethingHappened 
= true;
}


//Delphi中的寫法
//if assigned(FOnEvent) then
//FOnEvent(self,StrLog);
public void doWork() {
    
if (somethingHappened) {
     ie.interestingEvent();
    }

}

//
public static void main(String[] argc){
  CallMe cm
= new CallMe();  
  EventNotifier ev 
= new EventNotifier(cm);
  ev.doWork();

}


}

//回調函數要用到的接口
interface InterestingEvent {
     
public void interestingEvent();
}

//要實現調用函數,就要實現回 調函數接口(InterestingEvent )
class CallMe implements InterestingEvent {
private EventNotifier en;

public CallMe() {
    en 
= new EventNotifier(this);
}



public void interestingEvent() {
 System.out.println(
"ddd");
}


}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章