Reference Bo Master http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html
Anonymous inner class is also an inner class without a name
Because there is no name, the anonymous inner class can only be used once, and it is often used to simplify code writing
But there is also a precondition for using anonymous inner classes: You must inherit from a parent class or implement an interface
Example 1: Implementing an abstract method without using anonymous inner classes
Abstract classPerson { Public Abstract voideat ();} classChild extends Person { Public voideat () {System. out. println ("Eat something"); }} Public classDemo { Public Static voidMain (string[] args) {person P=NewChild (); P.eat (); }}
Operation Result: Eat something
As you can see, we inherit the person class with child, and then we implement an instance of child and transform it upward into a reference to the person class.
However, if the child class here is used only once, wouldn't it be a hassle to write it as a separate class?
The anonymous inner class is introduced at this time.
Example 2: Basic implementation of anonymous inner classes
Abstract classPerson { Public Abstract voideat ();} Public classDemo { Public Static voidMain (string[] args) {person P=NewPerson () { Public voideat () {System. out. println ("Eat something"); } }; P.eat (); }}
Operation Result: Eat something
As you can see, we directly implement the methods in the abstract class person in curly braces.
This allows you to omit the writing of a class
Also, anonymous inner classes can be used on interfaces
Example 3: Using an anonymous inner class on an interface
InterfacePerson { Public voideat ();} Public classDemo { Public Static voidMain (string[] args) {person P=NewPerson () { Public voideat () {System. out. println ("Eat something"); } }; P.eat (); }}
Operation Result: Eat something
As can be seen from the above example, as long as a class is abstract or an interface, the methods in its subclasses can be implemented using anonymous inner classes
The most common situation is in the implementation of multithreading, because to implement multithreading must inherit the thread class or inherit the Runnable interface
An anonymous inner class implementation of the instance 4:thread class
Public classDemo { Public Static voidMain (string[] args) {Thread T=NewThread () { Public voidrun () { for(inti =1; I <=5; i++) {System. out. print (i +" "); } } }; T.start (); }}
Operation Result: 1 2 3) 4 5
Example 5: Multithreading anonymous class implementation
Package com.think.classes; Public classDemo { Public Static voidMain (string[] args) { for(intI=0;i<6; i++) {Thread T=NewThread () {//It's equivalent to writing the Run method inside an anonymous class. Public voidrun () { for(inti =1; I <=5; i++) {System. out. print (i +" "); } } }; T.start (); } }}
Operating results: 1 1 2 3 4 5 1 2 3 4 5 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 (Inconsistent results)
An anonymous inner class implementation of the instance 6:runnable interface
Public classDemo { Public Static voidMain (string[] args) {Runnable R=NewRunnable () { Public voidrun () { for(inti =1; I <=5; i++) {System. out. print (i +" "); } } }; Thread T=NewThread (R); T.start (); }}
Operating Results 1 2 3 4 5
Anonymous inner class summary in Java