Inner Class (callback) in Java

Source: Internet
Author: User

The main functions of the inner class are as follows:

1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class

2. The method of the inner class can access all the data of the external class directly, including the private data

3. Functions implemented by internal classes can be implemented using external classes, but sometimes it is easier to use internal classes.

Second, the inner class can be divided into the following types:

    • member Inner class
    • Static Inner class
    • Method Inner Class
    • Anonymous inner class.

Three, 1, after defining the member inner class, you must use an external class object to create the inner class object, instead of going directly to the new inner class object, i.e. the inner class object name = Outer class object. New inner class ();

2, the outer class cannot directly use the members and methods of the inner class, you can first create an object of the inner class, and then access its member variables and methods through the objects of the inner class.

3, you can first create an object of an inner class and then access its member variables and methods through the objects of the inner class. HelloWorld.this.name

Iv. Java Callbacks

Callbacks are used for inter-tier collaboration, for example, as a driver, is a bottom-level, when he receives a data, in addition to complete the processing of this layer, will also be a callback to the upper application layer for further processing, which is common in hierarchical data communication.
In fact, callbacks and APIs are very close, and their commonalities are functions that are called across layers. But the difference is that the API is the lower level of the call to the upper layer, generally this function is known to the upper level, and the callback is the opposite, he is the upper level to provide to the bottom of the call, for the lower layer he is unknown, must be installed by the high level, this installation function is a low-tier API, After installation the lower level does not know the name of the callback, but it uses a function pointer to save the callback, when the call is required, simply reference the function pointer and related parameter pointers.
In fact: The callback is the function written in the upper layer, the lower layer through a function pointer to save the function, at the trigger of an event, the lower level through the function pointer to call the upper function.

A summary of the callback:
In Java, it is usually the person who writes another class or class library that specifies an interface, and then you implement this interface, and then pass an object of the interface as an argument to someone else's program, and other people's program will use that interface to invoke the function you write.

The following procedure will be a good indication of the process of callback execution:

 Public Interface CallBack {        // method   of executing callback operation     void  execute ();    }
1  Public classTools {2        3     /**  4 * Test function usage time, by defining the Execute method of the callback interface5      * @paramCallBack6 */   7      Public voidtesttime (CallBack CallBack) {8         LongBegin = System.currenttimemillis ();//Test Start Time9Callback.execute ();// Perform callback operations Ten         LongEnd = System.currenttimemillis ();//Test End Time OneSystem.out.println ("[Use time]:" + (End-begin));//Print usage Time A     }    -         -      Public Static voidMain (string[] args) { theTools tool =NewTools ();  -Tool.testtime (NewCallBack () { -             //defining the Execute method -              Public voidExecute () { +                 //Here you can add one or more methods to test the run-time   - Testobject.testmethod (); +             }    A         });  at     }    -         -}

The most common case of anonymous internal classes is the performance of the callback. A callback is a two-way invocation pattern, which means that the callee invokes the interface of the other when the interface is called.







Inner Class (callback) in 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.