Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of callback based on Java Interface

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Based on the example analysis of Java API callback, it is believed that many inexperienced people are at a loss about this. This article summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

Scenario where Java API callback generates API callback

In real life, the scenario of generating an interface callback is very simple. For example, I ask you to do something for me, and then you will notify me when you finish it. The action of "notify me" is the action of the interface callback. The API callback focuses on understanding and thinking. Another example is used in the following code demonstration. The teacher asks the students to do the class exercises. After the students finish the exercises, they tell the teacher.

What is an interface callback?

At the Java level, simply talk about interface callback: there is an interface A, and its implementation class A1, and another class B with interface A reference, because class B has a reference to interface A. after B executes the method, you can tell A that the method has been executed through the reference of interface A. Because An is an interface, the specific receiving and execution completion method is reflected in the implementation class A1.

Use in code

Taking the above teacher asking students to do class homework as an example, let's first create a callback interface and receive the message that the students have finished their class homework.

Public interface BackInterface {void backMethod ();}

Then define an implementation class Teacher, whose function is to let students do class exercises and receive notification that students have finished class exercises.

Public class Teacher implements BackInterface {/ / because the teacher wants to tell the students to do the class exercises, so this passes a student's quote private Student student; public Teacher (Student student) {this.student = student } / / the teacher told the students to do the class exercises public void doEvent () {System.out.println ("the teacher asked the students to do the exercises."); student.doPractice (this) } / / used to receive the message @ Override public void backMethod () {System.out.println ("the teacher received the message that the student has finished the class exercise");}}

Student class, the function is to do exercises, after finishing and tell the teacher (callback)

Public class Student {/ / students do class exercises and tell teachers public void doPractice (BackInterface BackInterface) {System.out.println ("students do exercises..."); System.out.println ("students tell teachers to finish exercises..."); BackInterface.backMethod ();}}

To test:

Public class BackDemo {public static void main (String [] args) {Teacher teacher = new Teacher (new Student ()); teacher.doEvent ();}}

Results:

We can see that the callback is complete. In the actual development process, in order to ensure efficiency, we often publish tasks asynchronously. In this case, the "teacher asks students to do class exercises" is made async. The concrete implementation is very simple, that is, to create a new thread, that is, to change the "student.doPractice (this)" code in the Teacher class to the following code:

New Thread (new Runnable () {@ Override public void run () {}}) .start ()

In addition, for the sake of code brevity, we often write Teacher classes in the form of anonymous inner classes, so the code will not demonstrate.

Java interface callback test

Interface class package com.java.demo.callBack_Interface; public interface CallBack {void getHValue (String s);} implementation class package com.java.demo.callBack_Interface; public class ApplyPro implements CallBack {public void getHValue (String sre) {String ss=sre; System.out.print (ss);}} callback package com.java.demo.callBack_Interface; public class TestC {CallBack callback; public TestC (CallBack callBack) {this.callback=callBack } public void getSpeak () {String str= "start acting"; callback.getHValue (str);}} implementation Test package com.java.demo.callBack_Interface; public class TestMain {public static void main (String [] args) {TestC tc=new TestC (new ApplyPro ()); tc.getSpeak ();}}

Display result: as long as it is printed to start the performance, it means that the callback is successful!

After reading the above, have you mastered the method of example analysis based on Java API callback? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report