Anonymous inner classes, i.e. inner classes without names.
When we write JAVA programs, we often create many classes, which can be reused. But sometimes, we create a class, but only need to use it once, so writing a class for it alone is somewhat troublesome. At this time, we can use anonymous inner classes to write this class, anonymous inner classes can only be used once.
· Anonymous inner classes have no names and therefore no constructors;
· There is a prerequisite for using anonymous inner classes: you must inherit a parent class or implement an interface.
Anonymous inner classes can be created by the following statement:
new InterfaceName(){......};
Or new SuperClassName(){...};;.
In {} is the concrete implementation of anonymous inner classes.
Here are a few examples to show the use of anonymous inner classes
1) There is no common way to use anonymous inner classes:
package com.tongye.anonymous; abstract class BaseClass{ abstract public void printMessage(); } class TestClass extends BaseClass { public void printMessage(){ System.out.println("Anonymous Internal Class Testing"); } } public class AnonymousClass { public static void main(String[] args){ TestClass testClass = new TestClass(); testClass.printMessage(); } } /* output: Anonymous Internal Class Testing */
This example does not use anonymous inner classes to create a new class that inherits abstract classes and implements the methods of abstract classes. Obviously, this new class can be called repeatedly or inherited.
2) Use anonymous internal class mode 1
package com.tongye.anonymous; abstract class BaseClass{ abstract public void printMessage(); } public class AnonymousClass { public static void main(String[] args){ BaseClass testClass = new BaseClass(){ public void printMessage(){ System.out.println("Anonymous Internal Class Testing"); } }; testClass.printMessage(); } } /* output: Anonymous Internal Class Testing */
This example seems to create an instance of an abstract class through the new operator. But we know that abstract classes can't create instances through the new operator, which is an illusion, and unlike ordinary classes, there is {}. In fact, an anonymous inner class is created here, and part of {} is the implementation details of the anonymous inner class. This anonymous class inherits the abstract class BaseClass. It implements the abstract method of BaseClass Abstract class. Of course, it can also add its own method body to it.
3) Using anonymous internal class mode 2
package com.tongye.anonymous; abstract class BaseClass{ abstract public void printMessage(); } public class AnonymousClass { public BaseClass testClass(){ // Create a BaseClass Type approach testClass(),The return value type is BaseClass type return new BaseClass(){ // Create an inheritance from BaseClass Objects of an anonymous class of public void printMessage(){ System.out.println("Anonymous Internal Class Testing"); } }; } public static void main(String[] args){ AnonymousClass anonymous = new AnonymousClass(); anonymous.testClass().printMessage(); } } /* output: Anonymous Internal Class Testing */
This example also uses anonymous inner classes, but the implementation here is somewhat different from that in 2). In this example, a method testClass() of BaseClass type is created first, and then the method testClass() returns an object of a class that implements the interface BaseClass using an anonymous inner class. The implementation of anonymous classes is still in {}.
4) Inheriting a parent class with a parametric constructor using an anonymous inner class
package com.tongye.anonymous; class BaseClass{ private int x; public BaseClass(int i){ x = i; } public void getx(){ System.out.println(x); } } public class AnonymousClass { public BaseClass baseClass(int x){ // Create a BaseClass Type approach baseClass(),The return value type is BaseClass type return new BaseClass(x){ // Create an inheritance from BaseClass Objects of an anonymous class of public void getx(){ System.out.println("Anonymous class testing"); super.getx(); } }; } public static void main(String[] args){ AnonymousClass anonymous = new AnonymousClass(); BaseClass baseClass = anonymous.baseClass(10); baseClass.getx(); } } /* output: Anonymous Internal Class Testing 10 */
If an anonymous inner class inherits a parent class that contains only a parametric constructor, it must be created with these parameters, and the corresponding content must be invoked using the super keyword in the implementation process. Although the BaseClass class here is a common class with concrete implementation, it is still used as a common "interface" by its exported class.
If you want to use an object defined externally in an anonymous inner class, you must ensure that its parameters are final.
Anonymous inner classes are somewhat similar to inheritance, but have some limitations compared to common inheritance. Anonymous inner classes can extend classes and implement interfaces, but they can't combine both; moreover, if they implement interfaces, they can only implement one interface.