Java 代表-如何?

Java 代表-如何?,java,android,function,interface,callback,Java,Android,Function,Interface,Callback,我正在尝试在我的android java项目中使用接口。回调接口似乎是C#委托的答案,但我无法理解它,我读过其他类似的问题,但同样,我无法让它满足我的需求或让它工作。我需要的是调用类a中的函数,该函数作为参数传递给类B。当类B完成任务时,通过调用先前保存在变量中的函数将结果返回给a: class A { B myBClass; public A() { myBClass = new B(); myBClass.doSomething(ResultFun

我正在尝试在我的android java项目中使用接口。回调接口似乎是C#委托的答案,但我无法理解它,我读过其他类似的问题,但同样,我无法让它满足我的需求或让它工作。我需要的是调用类a中的函数,该函数作为参数传递给类B。当类B完成任务时,通过调用先前保存在变量中的函数将结果返回给a:

class A {

   B myBClass;

   public A() {
       myBClass = new B();
       myBClass.doSomething(ResultFunction);
       //Continue doing other tasks
   }

   public void ResultFunction(<result parameters>) {
       //do something with the result from the task in myBClass
   }

}

class B {

    Function myCallback;

    public B() {

    }

    public void doSomething(ResultFunction) {
        myCallback = callback;
        //does something
        SomethingFinished();
    }

    private void SomethingFinished() {
        myCallback.call(<result of doSomething>);
    }

 }
A类{
B myBClass;
公共A(){
myBClass=newb();
myBClass.doSomething(ResultFunction);
//继续做其他任务
}
public void ResultFunction(){
//在myBClass中对任务的结果执行某些操作
}
}
B类{
函数myCallback;
公共图书馆B(){
}
公共void doSomething(ResultFunction){
myCallback=callback;
//做点什么
完成某事();
}
私有void SomethingFinished(){
myCallback.call();
}
}

有两种方法:

1)使用匿名类:

从类
B
doSomething方法中删除
ResultFunction
参数

并在类
A

   public A() {
       myBClass = new B() {
            @Override
            public void doSomething() {
                super.doSomething();
                //do here what you need what you want to do in B class
            }
       }
       myBClass.doSomething();
       //Continue doing other tasks
   }
2)使用
可调用接口

interface Callable {
    public void call();
}
传递它而不是
ResultFunction

class A {

   B myBClass;
   Callable callable = new Callable {
        @Override
        public void call() {
            //do here what you need what you want to do in B class
        }
   }

   public A() {
       myBClass = new B();
       myBClass.doSomething(callable);
       //Continue doing other tasks
   }

}

class B {

    Callable myCallback;

    public B() {

    }

    public void doSomething(Callable callable) {
        myCallback = callable;
        //does something
        SomethingFinished();
    }

    private void SomethingFinished() {
        myCallback.call();
    }

 }
有了lambdas,它看起来会更好一些

   public A() {
       myBClass = new B();
       myBClass.doSomething(()->{
          //do here what you need what you want to do in B class
       });
       //Continue doing other tasks
   }

我不明白你为什么不能做到以下几点:

公共A类{
私人B myBClass;
公共A(){
myBClass=newb();
结果=myBClass.doSomething();
resultFunction(结果)
//继续做其他任务
}
public void resultFunction(结果){
//在myBClass中对任务的结果执行某些操作
}
}
公共B级{
公共图书馆B(){
}
公共结果剂量测定法(){
//做点什么
}
}
如果出于任何原因,您必须这样做,那么您可以在Java 8中轻松地这样做:

公共A类{
B myBClass;
公共A(){
myBClass=newb();
myBClass.doSomething(A::resultFunction);
//继续做其他任务
}
public void resultFunction(结果){
//在myBClass中对任务的结果执行某些操作
}
}
公共B级{
私人消费者权益;
公共图书馆B(){
}
公共无效剂量测定(消费者回调){
myCallback=callback;
//做点什么
myCallback.accept(结果);
}
}

你不能从
ClassB
返回结果,这样你就可以从A调用你自己的方法了吗?