Home  >  Article  >  Java  >  Example analysis of Java implementation of interface callback method

Example analysis of Java implementation of interface callback method

黄舟
黄舟Original
2017-09-15 10:11:352421browse

The main content of this article is to show you the general usage of interface callbacks in Java programming through actual code. It has certain reference value. Friends who need it can understand

Interface callbacks refer to: You can use a certain If the reference of the object created by the class of an interface is assigned to the interface variable declared by the interface, then the interface variable can call the method of the interface implemented by the class. In fact, when an interface variable calls a method in the interface implemented by the class, it notifies the corresponding object to call the interface method. This process is called the interface callback of the object function.

General usage of Java interface callbacks: Implementing interfaces is actually similar to inheriting abstract classes, except that inheritance operates at the class level, and interfaces operate at the level of methods and constant collections. Interfaces are more flexible than abstract classes. Abstract and simpler. Implementing an interface can be seen as inheriting one or more specific methods and some constants. The specific rules of the interface will not be detailed here.

Why use interfaces and abstract classes? Because from many perspectives, this is in line with object-oriented design ideas, such as the opening and closing principle.

From the perspective of actual development, Java does not support multiple inheritance. In order to make up for this, the existence of interfaces is extremely important. Unlike inheritance, a class can implement multiple interfaces.

A major function of the interface is to implement callbacks. Callbacks are divided into synchronous callbacks and asynchronous callbacks. The difference is that asynchronous callbacks use multi-threading technology. When there are time-consuming operations in the callbacks, asynchronous callbacks need to be used. .

The following uses asynchronous callback as an example. The following uses Java writing as an example, and the same applies to Android.

1. General usage

Create a new entry class Main and create a new interface InterfaceExample


public class Main implements InterfaceExample{
  public static void main(String[] args) {
    System.out.println("------接口使用测试--------");
    InterfaceTest test = new InterfaceTest();
    //调用InterfaceTest的handleThings方法,并传递Main的实例
    test.handleThings(new Main());
    System.out.println("------异步回调测试--------");
  }
  @Override  //重写接口方法
  public void sendMessage(String string) {
    System.out.println("接口回调成功,利用 " + string + " 做一些事");
  }
}
 
//接口也可以写在一个独立的.java文件里
interface InterfaceExample {
  void sendMessage(String string);
}

Below is a new class InterfaceTest that initiates callbacks


public class InterfaceTest {
  //注意这里Main实例向上转型,接口变量引用了Main实例
  public void handleThings(InterfaceExample example) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        System.out.println("-----做一些事------");
        try {
          Thread.sleep(3000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        //回调接口方法
        example.sendMessage("接口传的参数");
      }
    }).start();
  }
}

Finally run output:


------接口使用测试--------
------异步回调测试--------
-----做一些事------
接口回调成功,利用 接口传的参数 做一些事

Asynchronous processing It can also be done when handleThings is called in Main.

2. Combine the anonymous inner class to implement the interface callback

The second method only needs to modify the Main class based on the first one


public class Main {
  public static void main(String[] args) {
    System.out.println("------接口使用测试--------");
    InterfaceTest test = new InterfaceTest();
    //调用InterfaceTest的handleThings方法,并使用实现了InterfaceExample接口的匿名内部类
    //在该匿名内部类中重写接口方法
    test.handleThings(new InterfaceExample() {
      @Override  //重写接口方法
      public void sendMessage(String string) {
        System.out.println("接口回调成功,利用 " + string + " 做一些事");
      }
    });
    System.out.println("------异步回调测试--------");
  }
}
interface InterfaceExample {
  void sendMessage(String string);
}

It can be seen that using anonymous inner classes can simplify the code and make the program structure clearer. So this usage is very common. For example, the click event of the view provided by the Android system uses this form of callback.

The output is the same:


------接口使用测试--------
------异步回调测试--------
-----做一些事------
接口回调成功,利用 接口传的参数 做一些事。

The above is the detailed content of Example analysis of Java implementation of interface callback method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn