Anonymous classes are classes that cannot have names, so there is no way to reference them. They must be declared as part of the new statement at creation time. This requires another form of new statement, as follows: new <class or interface> <body of class> This form of new statement declares a new anonymous class that extends a given class. Or implement a given interface. It also creates a new instance of that class and returns it as the result of the statement. The class to be extended and the interface to be implemented are the operands of the new statement, followed by the body of the anonymous class. If an anonymous class extends another class, its body can access the class's members, override its methods, etc., just like any other standard class. If an anonymous class implements an interface, its body must implement the interface's methods.
java code
Copy the code code as follows:
interface pr
{
void print1();
}
public class noNameClass
{
public pr dest()
{
return new pr(){
public void print1()
{
System.out.println("Hello world!!");
}
};
}
public static void main(String args[])
{
noNameClass c = new noNameClass();
pr hw=c.dest();
hw.print1();
}
}
PR can also be a class, but the method you call externally must be declared in your class or interface. Methods inside the anonymous class cannot be called from the outside.
Perhaps the most commonly used place for internal anonymous classes in Java is to add Listner to Frame.
as follows:
java code
Copy the code code as follows:
import java.awt.*;
import java.awt.event.*;
public class QFrame extends Frame {
public QFrame() {
this.setTitle(/"my application/");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
this.setBounds(10,10,200,200);
}
}
An internal anonymous class is to create an internal class, but it does not give you a name, that is, there is no variable that refers to the instance.
Copy the code code as follows:
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}
new is to create a WindowAdapter object, and the following {} indicates that the operation in the brackets acts on the default object name, and the following in the above Java program is a function body.
The purpose of this usage is to create an instance of an object and override one of its functions. You can find it by opening the WindowAdapter code. It is an abstract class. It is an implementation of the WindowListener interface. The parameter of Frame.addWindowListner(); is a WindowListner, and the implementation is to pass an anonymous class derived from WindowAdapter.
1. How to determine the existence of an anonymous class? I can't see the name, it feels like it's just an object created by new from the parent class, and there is no name for the anonymous class.
Let’s look at the pseudocode first
Copy the code code as follows:
abstract class Father(){
....
}
public class Test{
Father f1 = new Father(){ .... } //There is an anonymous inner class here
}
Generally speaking, when new an object, there should be a semicolon after the parentheses, that is, the statement of new object ends.
But it's different when there are anonymous inner classes. The parentheses are followed by braces, and the braces contain the specific implementation method of the new object.
Because we know that an abstract class cannot be new directly. There must be an implementation class before we can new its implementation class.
The above pseudocode indicates that new is the implementation class of Father, and this implementation class is an anonymous inner class.
In fact, splitting the above anonymous inner class can be
Copy the code code as follows:
class SonOne extends Father{
...//The code here is the same as the anonymous inner class above, the code in curly brackets
}
public class Test{
Father f1 = new SonOne();
}
2. Precautions for anonymous inner classes Note that the declaration of anonymous classes is done at compile time, and the instantiation is done at runtime. This means that a new statement in a for loop will create several instances of the same anonymous class, rather than creating one instance of several different anonymous classes.
When using anonymous inner classes, keep the following principles in mind:
・Anonymous inner classes cannot have constructors.
・Anonymous inner classes cannot define any static members, methods and classes.
・Anonymous inner classes cannot be public, protected, private, or static.
・Only one instance of an anonymous inner class can be created.
・An anonymous inner class must be behind new, and it is used to implicitly implement an interface or implement a class.
・Because anonymous inner classes are local inner classes, all restrictions on local inner classes take effect on them.
・Inner classes can only access static variables or static methods of outer classes.
this in anonymous classes and inner classes:
Sometimes, we will use some inner classes and anonymous classes. When this is used in an anonymous class, this refers to the anonymous class or inner class itself. At this time, if we want to use the methods and variables of the external class, we should add the class name of the external class
3. The role of anonymous inner classes
Java's inner classes are fundamentally different from nested classes in C++: C++'s nested classes do not have handles to wrapper classes. It only expresses the concept of encapsulation; but Java's inner class is different, it can access the members of the wrapping class (which means it has a handle to the wrapping class).
Anonymous inner classes are a simplified way of writing inner classes: return new Wrapper {
...
};
Equivalent to: Wrapped extends Wrapper {
...
}
return new Wrapped();
Is this the only role of anonymous inner classes?
Consider this case:
Copy the code code as follows:
interface ICount {
int count();
}
class Parent {
int i = 0;
int count() {
return i++;
}
}
There is a class Child, which not only wants to inherit the count() method of Parent, but also wants to implement the count method in the ICount interface. What should I do at this time? Inner classes can come into play:
Copy the code code as follows:
class Child extends Parent {
ICount getCount() {
return new ICount {
int i = 0;
int count() {
return (i *= 2);
}
}
}
}
look at this code
Copy the code code as follows:
public static void main(String[] args) {
theApp = new Analyzer();
SwingUtilities.invokeLater(new Runnable() { // Anonymous Runnable class
// object
public void run() { // Run method executed in thread
theApp.creatGUI(); // Call static GUI creator
}
});
}
public static void main(String[] args) {
theApp = new Analyzer(); // Create an object
SwingUtilities.invokeLater(new Runnable() { // Anonymous Runnable class
// An anonymous inner class that implements a thread
// Originally this method was to pass a Runnable type parameter // This can be achieved through this anonymous class method
// object
public void run() { // Run method executed in thread
theApp.creatGUI(); // Call static GUI creator
}
});
}