Callback functions in C + + and Java

Source: Internet
Author: User

First of all, because Java does not have the concept of pointers, so Java and C + + in the callback function, although the principle is similar, but the implementation method is different.

To tell the truth, if C + + is simple violence directly, Java is more like a little girl minced, I have too many times to understand this usage ....

1.c++

C + + is simple and straightforward, as long as the function address to be called is passed to the function pointer. As follows

  

#include <iostream>usingnamespace  std; void Pig () {   " Pig is barking!" " << Endl;} void Dog (void (* call) ()) {call    ();} int Main () {    dog (pig);}

It is really simple that the function pig () passes its own address to dog () and then calls the function through the function pointer (c + +, the function pointer Plus "()" equals the function that the function pointer points to).

2.java

In Java, the egg hurts, and sometimes it's not that obvious.

  

This is the project structure, the points are clear, the following is the code

A.java

  

 Package Javaapplication2;  Public Interface a {    publicvoid  bark ();}

B.java

  

 Package Javaapplication2;  Public class B {    private  a dog;      Public B (A dog) {        this. Dog= dog;        Dog.bark ();    }}

C.java

  

 Package Javaapplication2;  Public class Implements a{    @Override    publicvoid  bark () {        System.out.println ("Haha, I'm happy" );    }    }

Zhuhanshu.java

 

 Package Javaapplication2;  Public class Zhuhanshu {    publicstaticvoid  main (string[] args) {        new B (new  C ());}    }

And then, the principle is simple. is b to call a some of the methods to achieve some functions, but, perhaps A's method has been thought out, but temporarily not implemented, so B according to the method in a the return value and the parameters to be passed to use. When it really works, it's time for someone to implement the A method. So, C implements its method. Then, when called in the main function, the instance of C is passed over. If you don't understand it, you can debug it yourself by debugging the IDE's debug function, and you'll soon understand.

Callback functions in C + + and Java

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.