An example of closure in Java programming thought

Source: Internet
Author: User

An example of the idea of Java programming

 PackageInnerclass;Interfaceincrementable {voidincrement ();}/**be adjusted 1*///Very Simple to just implement the interfaceclassCallee1Implementsincrementable {Private inti = 0; @Override Public voidIncrement () {i++; System.out.println ("Callee1" +i); }}classmyincrement { Public voidincrement () {System.out.println ("Other Operation"); }    Static voidf (myincrement mi) {mi.increment (); }}//if your class must implement increment () in some//Inner classclassCallee2extendsMyincrementImplementsincrementable {Private inti = 0;  Public voidincrement () {Super. Increment (); I++; System.out.println ("Callee2:" +i); }    Private classClosureImplementsincrementable { Public voidincrement () {System.out.println ("Internal class implementation Interface");//1. I added this sentence. Callee2. This. increment ();//2. If you want to express a difference from an external class, why call the method of the external class to implement the same function, causing confusion}} incrementable getcallbackreference () {return NewClosure (); }}classCaller {Privateincrementable callbarckreference;  PublicCaller (incrementable ChB) {callbarckreference=ChB; }    voidGo () {callbarckreference.increment (); }} Public classCallbacks { Public Static voidMain (string[] args) {Callee1 C1=NewCallee1 (); Callee2 C2=NewCallee2 ();        MYINCREMENT.F (C2); Caller Caller1=NewCaller (C1); Caller Caller2=NewCaller (C2.getcallbackreference ());        Caller1.go ();        Caller1.go ();        Caller2.go ();    Caller2.go (); }}

An example of closure in Java programming thought

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.