简体   繁体   English

回调和接口之间的区别

[英]Difference between callback and interface

What is the difference between these two piece of code 这两段代码有什么区别

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

is that inner interface of View class, if so we know that interface can not be instantiated 是View类的内部接口,如果是这样我们知道接口无法实例化

And here 和这里

soInterface.getAnswers().enqueue(new Callback<SOAnswersResponse>() {
      @Override
      public void onResponse(Call<SOAnswersResponse> call, retrofit2.Response<SOAnswersResponse> response) {

      }

      @Override
      public void onFailure(Call<SOAnswersResponse> call, Throwable t) {

      }
  });

its callback that it required in the enqueue method, what this syntax define, is it interface "CallBack<>" defined some where and we are calling it here as inline interface, but again interface can not be instantiated like callback syntax says "new CallBack(){}" 它在enqueue方法中需要的回调,这个语法定义的是接口“CallBack <>”定义了一些在哪里我们在这里调用它作为内联接口,但是再次接口不能像回调语法那样实例化说“new CallBack” (){}”

What is the difference between these two piece of code 这两段代码有什么区别

Both are 2 different interface 两者都是2个不同的界面

View.OnClickListener - this interface helps you to listen to the View click action View.OnClickListener - 此界面可帮助您收听View单击操作

Callback<SOAnswersResponse> - Try to get the response from the server Callback<SOAnswersResponse> - 尝试从服务器获取响应

Both of them are interfaces: 它们都是接口:

  • CallBack is represents that the response(Callback) of Retrofit and when it is present you go into the onResponse or it failed to comeback (in the 10s time frame), be read(wrong parsing), or other reasons then onFailure will be executed. CallBack表示Retrofit的响应(Callback),当它出现时你进入onResponse或者它无法恢复(在10s的时间范围内),被读取(错误的解析),或者其他原因然后onFailure将被执行。

  • While onClickListener will be listening to button click. 而onClickListener将监听按钮点击。

you can implement an interface of onClick listener in the Activity or Fragment and use button.setOnClickListener(this) same for Retrofit. 您可以在Activity或Fragment中实现onClick侦听器的接口,并使用与Retrofit相同的button.setOnClickListener(this)。

Both View.OnClickListener and Callback are interfaces. View.OnClickListenerCallback都是接口。

OnClickListener is nested inside View class. OnClickListener嵌套在View类中。 Retrofit Callback is not nested. Retrofit Callback不是嵌套的。

This is the Callback interface doc: https://square.github.io/retrofit/2.x/retrofit/retrofit2/Callback.html 这是Callback接口doc: https//square.github.io/retrofit/2.x/retrofit/retrofit2/Callback.html

interface can not be instantiated 接口无法实例化

We instantiate an anonymous class here. 我们在这里实例化一个anonymous class ie We implement the interface as an anonymous class and instantiate that anonymous class on the fly. 即我们将interface实现为anonymous class并实时实例化anonymous class

From the Java doc : 来自Java doc

They [Anonymous class] enable you to declare and instantiate a class at the same time. 它们[匿名类]使您能够同时声明和实例化一个类。 They are like local classes except that they do not have a name. 他们就像当地的班级,除了他们没有名字。 Use them if you need to use a local class only once. 如果您只需要使用本地类一次,请使用它们。

Both classes in this case are interfaces with a callback. 在这种情况下,这两个类都是带回调的接口。 The anonymous class implements View.OnClickListener which has a callback on onClick . 匿名类实现View.OnClickListener ,它在onClick上有回调。 Same goes for the anonymous class implementing Callback<SOAnswersResponse>() . 实现Callback<SOAnswersResponse>()的匿名类Callback<SOAnswersResponse>() This time it has a typed parameter and two callbacks for onResponse and onFailure . 这次它有一个类型参数和onResponseonFailure两个回调。

The statement that interfaces can not be instantiated is true, but you can create an anonymous class of it. 接口无法实例化的声明是正确的,但您可以创建它的匿名类。 See Can we create an instance of an interface in Java? 请参阅我们可以在Java中创建接口实例吗?

You're correct, it is not possible to instantiate an interface. 你是对的,不可能实例化一个接口。 An interface is the definition of a set of methods that must be implemented by a concrete class. 接口是必须由具体类实现的一组方法的定义。

What your code is using, in both cases, is an anonymous inner class. 在两种情况下,您的代码使用的是匿名内部类。 The Java syntax allows you to specify that you would like a new instance of 'something' that implements the specified interface but you don't want to write all the code to do that. Java语法允许您指定您希望实现指定接口的“某事”的新实例,但您不希望编写所有代码来执行此操作。 Your code only provides the implementations of the abstract methods defined by that interface. 您的代码仅提供该接口定义的抽象方法的实现。

If you look at the class files for your application you will see something like MyClass$1.class, which is a class file generated synthetically by the compiler. 如果查看应用程序的类文件,您将看到类似MyClass $ 1.class的内容,它是由编译器综合生成的类文件。 The compiler creates a class called MyClass$1 that implements the interface you've specified with the methods you've defined. 编译器创建一个名为MyClass $ 1的类,该类实现您使用已定义的方法指定的接口。 The compiler will then make your code look something like this before compiling it: 然后,编译器将在编译之前使您的代码看起来像这样:

btn.setOnClickListener(new MyClass$1());

The same applies to the second example but with a different interface, etc. 这同样适用于第二个示例,但具有不同的界面等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM