接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口的方法。实际上,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程称为对象功能的接口回调。看下面示例。
简单一句话:把本来要A类要实现的方法,放到B中实现。通过接口来实现。
接口类(通过此类把方法转移):
interface HuiDiaoInterface {
public void DoThing();
}
实现方法类(最终实现方法类):
public class HuiDiaoDo implements HuiDiaoInterface{
public void DoThing() {
// TODO Auto-generated method stub
String x = "HuiDiaoDo a thing";
System.out.println(x);
}
}
业务类(调用方法的业务,持有接口类的引用):
public class Business {
static HuiDiaoInterface huidaiInterface = new HuiDiaoDo();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
huidaiInterface.DoThing();
}
}