java回调函数理解

java回调函数理解:

       简单理解:A对象调用B对象中的方法,该B中的方法又反过来调用A对象中的方法,此时A对象被调用的方法称之为A类的回调函数。

       白话理解:A向B提问了一个问题并告诉自己的联系方式,B解决了问题通过联系方式告诉了A。

A:
public class A
{
    public CallInterface mc;

    public void setCallfuc(CallInterface mc)
    {
       this.mc= mc;
    }

    public void call(){
       this.mc.method();
    }
}    

B:
public class B implements CallInterface
{
	private String context = "本函數中的內容!" ;
    public void method()
    {
       System.out.println("回调"+this.context);
    }

    public static void main(String args[])
    {
       A call = new A();
       call.setCallfuc(new B());
       call.call();
    }
}

接口:
public interface CallInterface
{
    public void method();

}

猜你喜欢

转载自see-you-again.iteye.com/blog/2251090