Interface in "Juincen" Java interface analogy with the callback function in JS

Source: Internet
Author: User

A few days ago in the left to fly voice, inadvertently realized that the interface in Java and JS commonly used callback function a bit similar, today here to tidy up ideas.

Interface,java in the interface, why there is this thing.

One, reservation statement

For example, two programmers a and b,a to write a program, which need to call the program written in B. At this point a can write an interface:

public interface demo{    void Deal ();      }

A method to invoke first "reservation declaration" in the interface: deal

Then B to perfect this interface, B wrote an implementation class to implement the demo interface.

A in their own business logic is only used to invoke the method of the interface can be, as to how to achieve the specific, regardless of him, leave B to do.

Two, API

When I was doing a voice flight, there was a logic that called the speech recognition API for voice-over speech.

There is a message Fly API interface Class Recognizerlistener, this interface is required for me to implement, there are several methods, I need to implement, and then the interface of the instance (object) to the message fly,

After the recognition is completed, call the method inside the interface (callback function), this time the function of the interface is reflected. (I don't know what I'm going to do after the recognition is done, so he reserved the interface, I realized to send the message to fly, he came to call)

When I return the result, I need to handle the result (string) of the recognition,

//1. Creating SpeechRecognizer Objects SpeechRecognizer miat= Speechrecognizer.createrecognizer ();//2. Set dictation parameters, see MSC Reference Manual speechconstant class Miat.setparameter ( Speechconstant.domain, "Iat"); Miat.setparameter (Speechconstant.language, "ZH_CN"); Miat.setparameter ( Speechconstant.accent, "Mandarin");//3. Start Dictation miat.startlistening (Mrecolistener);//Dictation listener Private Recognizerlistener Mrecolistener = new Recognizerlistener () {//interface method required to implement public void Onresult (Recognizerresult results, Boolean islast) {

//When I get the voice results here, I handle the results according to my business logic, and everyone writes differently. This method called the call, I only use the implementation of
DebugLog.Log ("Result:" +results.getresultstring ());} Session error callback interface public void OnError (Speecherror error) {error.getplaindescription (true)//Get Error code description}//start recording public void Onbe Ginofspeech () {}//volume value 0~30public void onvolumechanged (int volume) {}//end recording public void Onendofspeech () {}//extension with interface public void onEvent (int eventtype,int arg1,int arg2,string msg) {}};

In the development of the API often need to use the interface to the caller, because you do not know how the caller to implement his own business logic, then give him an interface to write to his own Bai!

The callback function in JS, such as the Ajax of jquery,

$.ajax ({    URL: "",    Type: "",    success:function () {      ///callback function, which implements its own logic       });

JS directly pass a function in the past,

Java needs to pass an interface object in the past, the system to invoke this interface object inside the method.

Interface in "Juincen" Java interface analogy with the callback function in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.