A brief analysis of Java anonymous inner class instances

  • 2020-04-01 01:27:15
  • OfStack

Anonymous classes are classes that cannot have names, so there is no way to reference them. They must be declared as part of a new statement when created. This takes another form of the new statement, as follows: new < Class or interface > < The body of the class > This form of new statement declares a new anonymous class that extends a given class or implements a given interface. It also creates a new instance of that class and returns it as a 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 principal can access its members, overwrite its methods, and so on, just like any other standard class. If an anonymous class implements an interface, its body must implement the interface's methods.
Java code
 
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 methods that you call externally must declare in your class or interface that the external cannot call methods inside anonymous classes
Perhaps the most common use of internally anonymous classes in Java is to add Listner to a Frame.
As follows:
Java code
 
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 internally anonymous class is one that creates an internal class but doesn't give you a name.
 
new WindowAdapter() { 
public void windowClosing(WindowEvent e) { 
dispose(); 
System.exit(0); 
} 
} 

New is a WindowAdapter object, followed by a {} to indicate that the operations in the parentheses act on the default object, and the above Java program is followed by a function body.
This usage creates an instance of an object and overrides a function of it. Open the Windows adapter code can be found. It is an abstract class. It is an implementation of the WindowListener interface. Frame. AddWindowListner (); Is a WindowListner, and the implementation is an anonymous class derived from WindowAdapter.
1. How to determine the existence of an anonymous class? Can not see the name, feel just the parent class new out of an object, there is no anonymous class name.
Let's look at the pseudo-code
 
abstract class Father(){ 
.... 
} 
public class Test{ 
Father f1 = new Father(){ .... } //So here's an anonymous inner class
} 

In general, a new object should be followed by a semicolon, that is, a new object.
But the appearance of anonymous inner class is not the same, curly braces followed by curly braces, curly braces is the new object's concrete implementation method.
Because we know that an abstract class cannot be new directly, we must have an implementation class before we can create its implementation class.
The above pseudocode is the implementation class representing new as Father, which is an anonymous inner class.
In fact, splitting the above anonymous inner class can be
 
class SonOne extends Father{ 
...//The code here is the same as in the above anonymous inner class, curly braces
} 
public class Test{ 
Father f1 = new SonOne() ; 
} 

2. Notes for anonymous inner classes
Note that anonymous classes are declared at compile time and instantiated at run time. This means that a new statement in the for loop creates several instances of the same anonymous class, rather than one instance of several different anonymous classes.
Here are a few principles to keep in mind when using anonymous inner classes:
, anonymous inner classes cannot have constructors.
, anonymous inner classes cannot define any static members, methods, or classes.
・ anonymous inner class can not be public, protected, private, static.
, only one instance of an anonymous inner class can be created.
, an anonymous inner class must be behind new to implicitly implement an interface or implement a class.
, because anonymous inner classes are local inner classes, all restrictions on local inner classes are effective on them.
, internal classes can only access static variables or static methods of external classes.
This in anonymous and inner classes:
Sometimes we use inner and anonymous classes. When used in an anonymous class, this refers to the anonymous class or the inner class itself. 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 have no handle to the wrapper class. Simply express a concept of encapsulation; But Java's inner class is different in that it can access the members of the wrapper class (which means it has a handle to the wrapper class).
Anonymous inner class is a simplified way of writing an inner class: return new Wrapper {
.
};
Wrapped extends Wrapper {
.
}
Return new Wrapped ();
Is that all anonymous inner classes do?
Consider a case like this:
 
interface ICount { 
int count(); 
} 
class Parent { 
int i = 0; 
int count() { 
return i++; 
} 
} 

What if you have a class Child that wants to inherit both Parent's count() method and the count method in the ICount interface? The inner class is ready for action:
 
class Child extends Parent { 
ICount getCount() { 
return new ICount { 
int i = 0; 
int count() { 
return (i *= 2); 
} 
} 
} 
} 

Look at this code
 
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
//  The original method was to pass one Runnable Type parameters  //  This can be done in the form of anonymous classes  
// object 
public void run() { // Run method executed in thread 
theApp.creatGUI(); // Call static GUI creator 
} 
}); 
} 

Related articles: