The inner class is not very well understood, but it is actually a class that contains another class.
As a person is composed of physical results, such as the brain, limbs, organs, or an internal class equivalent to one of the organs, such as the heart: it also has its own properties and behavior (blood, beating)
Obviously, it is not possible to use attributes or methods to represent a heart unilaterally, but a class
And the heart is in the body, just as the same inner class is inside the outside.
Example 1: The basic structure of an inner class?
123456789101112131415161718192021222324 |
//外部类
class Out {
private int age =
12
;
//内部类
class In {
public void print() {
System.out.println(age);
}
}
}
public class Demo {
public static void main(String[] args) {
Out.In in =
new Out().
new In();
in.print();
//或者采用下种方式访问
/*
Out out = new Out();
Out.In in = out.new In();
in.print();
*/
}
}
|
Run Result:12
From the above example, it is not difficult to see that the inner class actually seriously destroys the good code structure, but why use the inner class?
Because inner classes are free to use member variables of external classes (including private ones) without generating objects of external classes, this is the only advantage of inner classes
Like the heart can directly access the body's blood, rather than through the doctor to draw the blood
After compiling the program, two. class files are generated, respectively Out.class and Out$in.class
Where $ represents the out.in in the above program.
Out.in in = new Out (). The new in () can be used to generate an object of the inner class, which has two small knowledge points to note
1. The first out is to indicate which outer class the inner class object needs to be generated
2. An object of an outer class must be preceded to generate an object of the inner class, because the inner class is intended to access member variables in the outer class
Example 2: Variable Access form in an inner class?
1234567891011121314151617181920 |
class Out {
private int age =
12
;
class In {
private int age =
13
;
public void print() {
int age =
14
;
System.out.println(
"局部变量:" + age);
System.out.println(
"内部类变量:" +
this
.age);
System.out.println(
"外部类变量:" + Out.
this
.age);
}
}
}
public class Demo {
public static void main(String[] args) {
Out.In in =
new Out().
new In();
in.print();
}
}
|
Operation Result:
Local variables: 14
Internal class variable: 13
External class variables: 12
As you can see from Example 1, an inner class that has no member variables and local variables of the same name, the inner class accesses the member variables of the outer class directly without specifying Out.this. Property name
Otherwise, local variables in the inner class will overwrite the member variables of the outer class
The member variable that accesses the inner class itself is available with the this. property name, and accessing the member variables of the external class requires the use of Out.this. Property name
Example 3: Static inner class?
12345678910111213141516 |
class Out {
private static int age =
12
;
static class In {
public void print() {
System.out.println(age);
}
}
}
public class Demo {
public static void main(String[] args) {
Out.In in =
new Out.In();
in.print();
}
}
|
Run Result:12
As you can see, if you statically internal static, the inner class can only access static member variables of the outer class, with limitations
Second, because the inner class is statically, the out.in can be viewed as a whole, and can be directly new to the object of the inner class (access to static through the class name, it doesn't matter if the external class object is generated)
Example 4: Private inner class?
123456789101112131415161718192021222324 |
class Out {
private int age =
12
;
private class In {
public void print() {
System.out.println(age);
}
}
public void outPrint() {
new In().print();
}
}
public class Demo {
public static void main(String[] args) {
//此方法无效
/*
Out.In in = new Out().new In();
in.print();
*/
Out out =
new Out();
out.outPrint();
}
}
|
Run Result:12
If an inner class only wants to be manipulated by a method in an external class, you can use private to declare the inner class
In the above code, we have to create an in class object inside the out class, and we can no longer use out.in in = new Out (). New in () creates an object of the inner class
That is, the inner class at this point is controlled only by the outer class
As is, my heart can only be controlled by my body, others cannot access it directly
Example 5: Method inner class?
1234567891011121314151617181920 |
class Out {
private int age =
12
;
public void Print(
final int x) {
class In {
public void inPrint() {
System.out.println(x);
System.out.println(age);
}
}
new In().inPrint();
}
}
public class Demo {
public static void main(String[] args) {
Out out =
new Out();
out.Print(
3
);
}
}
|
Operation Result:
3
12
In the above code, we move the inner class into the method of the outer class, and then regenerate it into an inner class object in the method of the outer class to invoke the inner class method
If we need to pass in a parameter to a method in the outer class at this point, then the method parameter of the outer classes must use the final definition
As for final, there's no special meaning here, it's just a representation.
The inner class of Java learning