Details and examples of Java basic internal classes
- 2020-06-19 10:09:53
- OfStack
Details and examples of Java basic internal classes
Inner classes are not easy to understand, but in plain English, one class contains another class
Just as a person is composed of the brain, limbs, organs and other bodily results, an internal class is equivalent to one of the organs in it, such as the heart: it also has its own properties and behaviors (blood, beating).
Obviously, you can't represent a heart as a property or method alone here, you need a class instead
And the heart is in the body, just as the inner is in the outer
Example 1: The basic structure of the inner class
// Outside class
class Out {
private int age = 12;
// The inner class
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();
// Or it can be accessed in the following way
/*
Out out = new Out();
Out.In in = out.new In();
in.print();
*/
}
}
Operation result: 12
As you can see from the above example, inner classes are a serious breach of good code structure, but why use them anyway?
Because inner classes are free to use the member variables of the outer class (including private ones) without generating objects of the outer class, this is the only advantage of inner classes
Just as the heart can access blood directly from the body, instead of drawing it through a doctor
After compiling the program, two.class files are generated, Out.class and Out$In.class
The $represents the one in Out.In in the above program.
Out. In in = new Out().new In() can be used to generate objects of inner classes, with two small points to note
1. Out is used to indicate which external class object of the inner class needs to be generated
2. You must have an object of an external class to generate an object of an internal class, because the purpose of the internal class is to access the member variables of the external class
Example 2: Variable access form in inner class
class Out {
private int age = 12;
class In {
private int age = 13;
public void print() {
int age = 14;
System.out.println(" Local variables: " + age);
System.out.println(" Internal class variables: " + this.age);
System.out.println(" External class variables: " + Out.this.age);
}
}
}
public class Demo {
public static void main(String[] args) {
Out.In in = new Out().new In();
in.print();
}
}
Operation results:
Local variable: 14
Inner class variable: 13
External class variable: 12
As you can see from example 1, in the absence of a member variable with the same name and a local variable, the inner class accesses the member variable of the outer class directly without specifying the Out.this. attribute name
Otherwise, local variables in the inner class overwrite member variables of the outer class
Access to the member variable of the inner class itself is provided by the this. attribute name, while access to the member variable of the outer class requires the Out. this. attribute name
Example 3: Static inner class
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();
}
}
Operation result: 12
As you can see, if you use static to statically inhouse, then the inner class can only access the static member variables of the outer class, which is limited
Second, because the inner class is static, Out. In can be viewed as a whole, and the object of the inner class can be directly identified by new (static is accessed by the class name, it doesn't matter whether the external class object is generated or not).
Example 4: Private inner class
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) {
// This method is invalid
/*
Out.In in = new Out().new In();
in.print();
*/
Out out = new Out();
out.outPrint();
}
}
Operation result: 12
If an inner class only wants to be operated on by methods in the outer class, you can declare the inner class using private
In the above code, we must generate the object of the In class inside the Out class to operate, and we can no longer use Out.In in = new Out().new In() to generate the object of the inner class
That is, the inner class at this point is controlled only by the outer class
Like, my heart can only be controlled by my body, and no one else has direct access to it
Example 5: Method inner class
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 results:
3
12
In the above code, we move the inner class to the method of the outer class, and then regenerate an inner class object in the method of the outer class to invoke the inner class method
If at this point we need to pass parameters into the methods of the external class, the method parameters of the external class must be defined using final
As for final, there is no special meaning here. It is just a representation.
Thank you for reading, I hope to help you, thank you for your support to this site!