Today, when I look at the template method design pattern, we find that in the interface callback, the anonymous class has a new interface. Thought it was weird, and I wrote a little program to test it.
// Hibernatecallback is an interface Public void Saveuser (final user user) { execute (new hibernatecallback () { public Object Doinsession (Session session) { session.save (user); return NULL ; } }); }
Here is the test code
Public Interfaceinterface{voidprint ();} Public classTest {Static voidUpdateUser () {Execute (NewInterface () {@Override Public voidprint () {System.out.println ("I am an Interface"); } }); } Static voidExecute (Interface i) {i.print (); } Public Static voidMain (string[] args) {updateUser (); }}
The output is: I am an interface
Anti-compilation files for anonymous classes
From the anti-compilation file is not a new interface, but this anonymous class implementation of the interface, and then passed on as a parameter of the past.
Interface cannot be instantiated