Object-oriented (Local internal class and anonymous internal class), object-oriented Anonymous
/**
* Created by rabbit on 2014-08-05.
* When the internal class is defined locally,
* 1. It cannot be modified by a member modifier.
* 2. You can directly access members in the external class because they also hold
* References in the external class. However, it cannot access the local location of the object.
. Only local variables modified by final can be accessed.
*
* Anonymous internal class
* 1. An anonymous internal class is short for an internal class.
**/
Class outer4
{
Int x = 3;
Void method ()
{
Final int y = 5;
Class inner
{
Void function ()
{
System. out. println (y );
}
}
New inner (). function ();
}
}
public class innerclassDemo4 {
public static void main(String [] args)
{
new outer4().method();
}
}
What are the characteristics and functions of local and anonymous internal classes in JAVA? I 'd better explain it in detail.
Java internal class
There are four types: Member internal class, local internal class, static internal class, and anonymous internal class.
1. Member internal class: a member of an external class, which is in parallel with the attributes and methods of the external class.
Note: static variables cannot be defined in the member's internal class, but all members of the external class can be accessed.
Public class Outer {
Private static int I = 1;
Private int j = 10;
Private int k = 20;
Public static void outer_f1 (){
// Do more something
}
Public void out_f2 (){
// Do more something
}
// Member internal class
Class Inner {
// Static int inner_ I = 100; // static variables cannot be defined in the internal class
Int j = 100; // instance variables of internal and foreign departments can coexist.
Int inner_ I = 1;
Void inner_f1 (){
System. out. println (I); // If the variable of the external class does not have the same name as the variable of the internal class, you can directly use the variable name to access the variable of the external class.
System. out. println (j); // directly use the variable name to access the internal class's own variables in the internal class
System. out. println (this. j); // you can also use "this. variable name" in the internal class to access internal class variables.
// Access the instance variables with the same name as the internal class in the external class. You can use "external class name. this. variable name ".
System. out. println (k); // If the variable of the external class does not have the same name as the variable of the internal class, you can directly use the variable name to access the variable of the external class.
Outer_f1 ();
Outer_f2 ();
}
}
// Access the internal class of the member using non-static methods of the external class
Public void outer_f3 (){
Inner inner = new Inner ();
Inner. inner_f1 ();
}
// The static method of the external class accesses the internal class of the member, which is the same as the internal class of the external class access member.
Public static void outer_f4 (){
// Step 1 create an external Class Object
Outer out = new Outer ();
// *** Step 2: create an internal class object based on the external Class Object ***
Inner inner = out. new Inner ();
// Step 3: Access the internal class
Inner. inner_f1 ();
}
Public static void main (String [] args ){
Outer_f4 ();
}
}
Advantages of member Internal classes:
(1) The internal class acts as a member of the external class and can access the private members or attributes of the external class. (Even if the external class is declared as PRIVATE, it is still visible to internal classes .)
(2) Use an internal class to define inaccessible attributes of an external class. In this way, the external class has lower access permissions than the external class's private.
Note: Internal classes are a compilation concept. Once compiled successfully, they become completely different. For an external class named outer and its internal defined internal class named inner. After compilation is complete, the outer. class... remaining full text appears>
Different solutions for local internal classes and anonymous internal classes
// The anonymous internal class does not even have a class name (actually ).
Therefore, the anonymous internal class has only one instance and is not referenced.
For example, you cannot reuse the anonymous subclass of the Thread class below.
Internal class. The internal class has a name, but it is inside the class.
It can access all data of the external class, including private.
Even if an internal class is in a method, the local variables of the method can also be used by the internal class.
In general, the effect of the anonymous internal class is not much different from that of the internal class.
It is often used in event listening and thread applications.