The anonymous class in Java has a magical usage, as shown in the following code example:
Package contract; Public Interface Isay { void SayHello ();}
Above is a simple interface, below is how to use:
Package Jimmy; Import contract. Isay; Public class Program { publicstaticvoid main (string[] args) { New Isay () { publicvoid SayHello () { System.out.println ( "Hello java!" ); } }; }}
At first glance, it seems as if "an interface instance is new directly" without providing an interface implementation.
Fortunately, it's just an illusion.observing the class output in the bin directory, you will find a similar program1.CLASSOfTextThing,Such asFruitAnti -CodecTranslatedViewSrebrenicaOneUnder,HairIsOriginalToIsCodecTranslatedManagerSelf -Move Sheng One class pr< Span id= "mathjax-span-98" class= "Mi" >og ra m 1:
Package Jimmy; Import contract. Isay; Import Java.io.PrintStream; class Implements isay{ publicvoid SayHello () { System.out.println (" Hello java! " ); }}
If there are situations where you just need to create an implementation class for an interface temporarily, the "tricks" above can be used to simplify the code.
Http://www.cnblogs.com/yjmyzz/p/3448330.html
Java: Using anonymous class direct new interface