Implementation of callback functions in java, php, and javaScript

Source: Internet
Author: User
Tags call back

The basic idea of callback functions can be implemented by any program is the same. The implementation is slightly different. The following describes how to implement callback functions through java, php, and javaScript, speak nonsense, action!

Java callback functions are generally implemented by defining interfaces. Of course, abstract classes can also be defined.

The details are as follows:

<1> interface definition:

Package com;
/*
* Define an Interface
*/
Public interface CallBack {
/* Define an abstract Method */
Abstract void doExec (String str );
}

<2> define a class:

Package com;
Import com. CallBack;
/**
* Define a class
* @ Author Administrator
*/
Public class DoExcute {
/**
* This method uses the callback method.
* @ Param name: Just upload a parameter.
* @ Param callBack Parameters
*/
Public static void doExcute (String name, CallBack callBack ){
CallBack. doExec (name );
}
Public static void main (String args [])
{
String name = "mo Xiaoming ";
// The first call
DoExcute. doExcute (name, new CallBack (){
Public void doExec (String strCal ){
String str = "Hello everyone, my name is:" + strCal + "just call me James. Hey hey .";
// Perform specific operations
// Here is the simplest printing function.
System. out. println (str );
}
});

// The second call implements another function
Doexcute. doexcute (name, new callback (){
Public void doexec (string strcal ){
// Execute the desired function, or call a new method here
//, Even a new class is possible. The specifics depend on how rational your design is.
P (strcal );
}
});
//

}
// A simple test
Private Static void P (string name)
{
String STR = ("mo Xiaoming". Equals (name ))? "Handsome guy! ":" A beast ";
System. out. println (name + str );
}
}

The callback function of PHP is currently tested. I only know how to do this.

<? Php
/*
* Define a class
*/
Class DoHepler {
/*
* This method executes a function, which is provided to the callback function for implementation.
* Parameter callBack: callBack function
*/
Public static function doExec ($ callBack)
{
$ CallBack ();
}
}
// For the first call, input the callback function callBack0:
DoHepler: doExec ('callback0 ');
// The second call passes in the callback function callBack1:
DoHepler: doExec ('callback1 ');
// Callback function callBack0
Function callBack0 ()
{
// The complicated logic is left for you to handle ......
Echo 'run now! ';
}
// Callback function callback1
Function callback1 (){
// The complicated logic is left for you to handle ......
Print ("<HR> goto Beijing! ");
}
?>

In JS, the approach in. JS is slightly more flexible.

<Script language = "jscript">
//
Function A (p, callback ){
// It can contain only a few parameters.
Alert (P );
Callback ();
}
Function _ callback_0 (){
// A series of changes may be made here
Alert ("call back! ");
}

A ("<br> 1." ,__ callback_0 );
// Directly pass the function as a parameter
A ("<br> 2.", Function C () {alert ("Go go !!!!!!!!! ");});
// Directly pass the function as a parameter
A ("<HR> 3.", Function C (){
Var I = 25;
Var c = "Tom ";
Var x = "male ";
Document. write ("name:" + c + ", Gender:" + x + ", age:" + I );
});
</Script>

Go to: http://ming206.javaeye.com/blog/207746

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.