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