++ ++ ++
Internal class, local internal class, anonymous internal class
++ ++ ++
/**
* <Internal class>: class defined in another class.
* Usage reason:
1. Internal class method, which can access the data in the scope of the <external class> definition, including private data
2. The internal class can be hidden from <other classes in the same package>
3. When you want to define a <callback function> and do not want to write a lot of code, use the <Anonymous class> simple
**/
Public class talkingclock {
Private int interval;
Private Boolean beep;
Public void start (){
...
}
// Internal class
Public class timeprinter implements actionlistener {
@ Override
Public void actionreceivmed (actionevent event ){
Date Now = new date ();
...
// Outerclass. This. ATTR ---> use <external class> reference in <internal class>
If (talkclock. This. Beep) Toolkit. getdefatooltoolkit (). Beep ();
}
}
}
/**
* <Local internal class> ----- define <internal class> in a method of <external class>
**/
Public void start (){
// 1. The local class cannot be declared using public/private, and its effect is limited to the <block> of the local class.
// 2. Local class [advantage]: completely hidden from <block>, even if <external class> other Code cannot be accessed
Class timeprinter implements actionlistener {
@ Override
Public void actionreceivmed (actionevent event ){
Date Now = new date ();
...
}
}
Actionlistener listener = new timeprinter ();
}
/**
* <Anonymous internal class> (Anonymous class) ------ no class name; therefore, no constructor is allowed;
* <Anonymous internal class> is an in-depth step towards <partial internal class>. If you only create one object for this class, you do not need to name it.
**/
Public void start (INT interval, final Boolean beep ){
Actionlisten listener = new actionlistent (){
@ Override
Public void actionreceivmed (actionevent event ){
Date Now = new date ();
....
}
}
}