Java Callback Test

Source: Internet
Author: User

Callback: When you call a method of Class B in Class A, the Class A invokes a method that has a specific implementation in another class (for example, Class B, usually an interface).

/** Listener Interface */interface listener{public    void callbacks (); /** by listening class */class a{    Listener mlistener;    Public A () {} public    a (Listener Listener) {        this.mlistener= Listener;    }        /** Set Listener *    /A Setlistener (Listener Listener) {        this.mlistener= Listener;        return this;    }    /** Callback interface *    /Void Call () {        if (mlistener== null) return;        Mlistener.callbacks ();    }} /** Main class */public final class main implements listener{public    static void main (String []args) {        new Main (). Start () ;    }    private void Start () {        new A (). Call ();//null Listener event        New A (this). Call ();        New A (this). Setlistener (New Listener () {public            void callbacks () {                System.out.println ("This is new callbacks Method ");            }        }). Call ();//Reset Listener        }    /** the specific implementation of the Listener event */public    void Callbacks () {        System.out.println ("Call Me");}    }    

  

Java callback test

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.