1、回調機制概述java
public interface CallBack { public void solve(int result); }
public class StudentA implements CallBack{ private StudentB mStuB; public StudentA(StudentB mStuB){ this.mStuB = mStuB; } public void askQuestion(final int a,final int b){ new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub mStuB.executeMessage(StudentA.this, a, b); } }).start(); } @Override public void solve(int result) { // TODO Auto-generated method stub System.out.println(result); } }
(3)Class B的實現ide
public class StudentB { public void executeMessage(CallBack callBack,int a,int b){ System.out.println("A的問題是:"+a +" + "+ b+"=?"); int result = a + b; callBack.solve(result); } }
(4)值的初始化this
public class CallBackTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a = 1; int b = 1; StudentB mStuB = new StudentB(); StudentA mStuA = new StudentA(mStuB); mStuA.askQuestion(a,b); } }