Java Anonymous class and internal class.
Basic Theory:
-----------------------------------------------------
Java internal class: the definition of an internal class is defined within another 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-drivenProgram.
In fact, it is only intended to define callback-further, it is event-driven.
Interface and callback: a common programming mode is the callback mode. In this mode, you can specify the method of the callback object when a specific time occurs.
--------------------------------------------------
Note:
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. For example:
/** An application to demonstrate the use of internal Classes */
/** Class Outer */
Class Outer
{
Private Static int size;
/** Internal class inner Declaration */
Public class inner
{
Private int size;
/** Method dostuff ()*/
Public void dostuff (INT size)
{
Size ++; // access local variables
This. Size ++; // access the member variables of its internal class
Outer. This. Size ++; // access the member variables of its external class
System. Out. println (size + "" + this. Size + "" + outer. This. size );
}
} // Internal class inner ends
/** Instance method testinner () method defined in class Outer */
Public void testinner ()
{
Inner I = new inner ();
I. dostuff (5 );
}
/** Main () method */
Public static void main (string [])
{
Outer o = new outer ();
O. testinner ();
}
} // Class outer ends