Anonymous inner classes are suitable for creating classes that only need to be used once, such as the Command object required in command mode. The syntax of anonymous inner classes is a bit strange. When an anonymous inner class is created, an instance of the class is immediately created. The class definition disappears immediately, and the anonymous inner class cannot be reused.
The format for defining anonymous inner classes is as follows:
new 父类构造器(参数列表)|实现接口() { //匿名内部类的类体部分 }
As can be seen from the above definition, anonymous inner classes must inherit a parent class or implement an interface, but they can only inherit at most one parent class or implement an interface.
There are two more rules about anonymous inner classes:
1) Anonymous inner classes cannot be abstract classes, because when the system creates an anonymous inner class, it will immediately create an object of the inner class. Therefore, anonymous inner classes are not allowed to be defined as abstract classes.
2) Anonymous inner classes do not define constructors. Because anonymous inner classes do not have a class name, they cannot define a constructor, but anonymous inner classes can define instance initialization blocks.
Use instance initialization blocks to complete what the constructor needs to accomplish.
The most common way to create an anonymous inner class is to create an object of an interface type, as shown in the following program:
interface Product{ public double getPrice(); public String getName(); } public class TestAnonymous{ public void test(Product p){ System.out.println("购买了一个"+p.getName()+",花掉 了"+p.getPrice()); } public static void main(String[]args){ TestAnonymous ta = new TestAnonymous(); ta.test(new Product(){ public double getPrice(){ return 567; } public String getName(){ return "AGP显卡"; } }); } }The TestAnonymous class in the above program defines a test method, which requires a Product object as a parameter, but Product is just an interface, and objects cannot be created directly. Therefore, consider creating an object of the Product interface implementation class and passing it into this method. --- If this Product interface implementation class needs to be used repeatedly, an independent object should be defined through the implementation class. class; if this Product interface implementation class only needs to be used once, you can use the method in the above program to define an anonymous inner class.
As you can see in the above program, the class keyword is not required to define an anonymous class. Instead, when defining an anonymous inner class, the object of the anonymous inner class is directly generated. The code part in bold above is the class body part of the anonymous class.
Since anonymous inner classes cannot be abstract classes, anonymous inner classes must implement all abstract methods contained in its abstract parent class or interface.
The above code for creating Product implementation class objects can be split into the following code:
class AnonymousProduct implements Product{ public double getPrice(){ return 567; } public String getName(){ return "AGP显卡"; } } ta.test(new AnonymousProduct());
abstract class Device{ private String name; public Device(){ } public Device(String name){ this.name = name; } public abstract double getPrice(); //此处省略了name属性的setter和getter方法 } public class AnonymousInner{ public void test(Device d){ System.out.println("购买了一个"+d.getName()+",花掉了"+d.getPrice()); } public static void main(String[] args){ AnonymousInner ai = new AnonymousInner(); //调用有参数的构造器创建Device匿名实现类的对象 ai.test(new Device("电子示波器"){ public double getPrice(){ return 67; } }); //调用无参数的构造器创建Device匿名实现类的对象 Device d = new Device(){ //初始化块 { System.out.println("匿名内部类的初始化块..."); } //实现抽象方法 public double getPrice(){ return 56; } public Sting getName(){ return "键盘"; } }; ai.test(d); } }
When creating an anonymous inner class, all abstract methods in the interface or abstract parent class must be implemented. If necessary, you can also override the ordinary methods in the parent class. For example, in the second bold code part of the program above, the anonymous inner class overrides the getName method of the abstract parent class Device class, and the getName method is not abstract. method.
If the anonymous inner class needs to access the local variables of the outer class, it must use the final modifier to modify the local variables of the outer class, otherwise the system will report an error.
interface A{ void test(); } public class TestA{ public static void main(Strign[] args){ int age = 0; A a = new A(){ public void test(){ //下面语句将提示错误:匿名内部类内访问局部变量必须使用final修饰 System.out.println(age); } }; } }
For more articles related to anonymous internal classes, please pay attention to the PHP Chinese website!