What is the difference between Static Nested Class and Inner Class?
Nested Class (generally C ++) and Inner Class (generally JAVA ). The biggest difference between Java internal classes and C ++ Nested classes is whether there are external references.
Note: The static internal Class (Inner Class) means 1 to create a static internal Class object, no need for an external Class Object 2 can not access an external Class object from an object of a static internal Class
Can Anonymous Inner Class (Anonymous internal Class) be extends (inherited) other classes, and implements (implemented) interface (interface )?
Inner Class (internal Class) is defined in the Class.
Nested Class (Nested Class) is a static internal Class. 1. To create nested class objects, you do not need the objects of its peripheral class. 2. Non-static peripheral class objects cannot be accessed from nested class objects.
Anonymous Inner Class (Anonymous internal Class) Anonymous internal Class is an internal Class without a name.
Anonymous internal classes cannot be extends (inherited) other classes, but an internal class can be used as an interface and implemented by another internal class.
Nested classes can be used as internal classes of interfaces. Normally, you cannot place any code inside the interface, but the nested class can be part of the interface because it is static. Only placing Nested classes in the interface namespace does not violate the interface rules.
The internal class is inherited. Because the internal class has a secret reference pointing to the peripheral class object, this secret reference must be initialized when inheriting the internal class. The solution is enclosingClassReference. super (); Syntax. Let's take a look at the Code:
Class Outer
...{
Class Inner
...{
}
}
Class AnoClass extends Outer. Inner
...{
AnoClass (Outer wi)
...{
Wi. super ();
}
}
Anonymous Class)
When the class name of an internal class is used only once when such an object is created, and the new class to be generated must inherit from an existing parent class or implement an interface, in order to consider using an anonymous class, the anonymous class itself is unknown, so it does not have a constructor. It needs to explicitly call the constructor of a parent class without parameters, and override the method of the parent class.
............
F. addMouseMotionListener (new MouseMotionAdapter () {// Anonymous class starts
Public void mouseDragged (MouseEvent e ){
String s = "Mouse dragging: x =" + e. getX () + "Y =" + e. getY ();
Tf. setText (s );}
}); // End of the anonymous class
The reason is:
1. An internal class object can access the implementation of the object created for it, including private data. That is, an internal class instance is privileged to the instance of the class that contains it.
2. for other classes in the same package, the internal class can be hidden. In other words, the internal class, regardless of the method visibility, may be public. In addition to the inclusive class, other classes cannot use it.
3. Anonymous internal classes can easily define callbacks.
4. Use internal classes to easily write event drivers.
In fact, it is only intended to define callback-further, it is event-driven.
When using anonymous internal classes, remember the following principles:
· No constructor is allowed for anonymous internal classes.
· Anonymous internal classes cannot define any static members, methods, and classes.
· Anonymous internal classes cannot be public, protected, private, or static.
· Only one instance of the anonymous internal class can be created.
· An anonymous internal class must be behind new and be used to implicitly implement an interface or implement a class.
· Because the anonymous internal class is a local internal class, all restrictions on the local internal class take effect.
This in the anonymous class and internal class:
Sometimes, some internal and anonymous classes are used. When this is used in an anonymous class, this refers to the anonymous class or internal class itself. If we want to use external class methods and variables, we should add the Class Name of the external class.
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/welcomejzh/archive/2008/01/09/2031428.aspx