Tag: Call represents X11 key Stat otherwise [] class variable Containe
Summary of internal classes in Java
Inner classes are not very well understood. But frankly, it's actually a class that also includes another class.
Like a person is composed of the brain, limbs, organs and other body results. The inner class is equivalent to one of the organs in it, 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 to have a class
And the heart is in the human body, as the same inner class is inside the outer
Example 1: The basic structure of an inner class
| 123456789101112131415161718192021222324 |
//外部类class Out { privateint age = 12; //内部类 classIn { publicvoid print() { System.out.println(age); } }}public class Demo { publicstatic void main(String[] args) { Out.In in =new Out().newIn(); in.print(); //或者採用下种方式訪问 /* Out out = new Out(); Out.In in = out.new In(); in.print(); */ }} |
Execution Result: A
It is not difficult to see from the above examples. The inner class actually seriously destroys the good code structure, but why use internal classes?
Because the inner class can arbitrarily use the member variables of the outer class (including the private ones) without generating an object of the outer class, this is the only advantage of the inner class
As the heart can directly access the body's blood, rather than through the doctor to draw the blood
After the program compiles, it produces two. class files, each of which is out.class and Out$in.class
The $ represents the out.in in the above program.
Out.in in = new Out (). New in () can be used to generate an object of the inner class. There are two small knowledge points to be aware of in such a way
1. The first out is to indicate which outer class the inner class object needs to be generated in
2. You must have an object of an external class to generate an object of the inner class, because the inner class acts to access member variables in the external class.
Example 2: Variable Access form in an inner class
| 1234567891011121314151617181920 |
class Out { privateint age = 12; classIn { privateint age = 13; publicvoid print() { intage = 14; System.out.println("局部变量:"+ age); System.out.println("内部类变量:"+ this.age); System.out.println("外部类变量:"+ Out.this.age); } }}public class Demo { publicstatic void main(String[] args) { Out.In in =new Out().newIn(); in.print(); }} |
Execution Result:
Local variables: 14
Internal class variable: 13
External class variables: 12
Can be found from instance 1. Inner classes without a member variable and local variable of the same name, the inner class directly visits the member variables of the external class without specifying out.this. Property name
Otherwise, local variables in the inner class will overwrite the member variables of the outer class
Access to the member variables of the inner class itself can be used for this. Property name, visiting the member variables of the external class need to use Out.this. Property name
Example 3: Static inner class
| 12345678910111213141516 |
class Out { privatestatic int age = 12; staticclass In { publicvoid print() { System.out.println(age); } }}public class Demo { publicstatic void main(String[] args) { Out.In in =new Out.In(); in.print(); }} |
Execution Result: A
To be able to see that, assuming that static is statically inside, the inner class can only access static member variables of the external class, with limitations
Second, because the inner class is statically, the out.in can be viewed as a whole. The ability to directly new the object from the inner class (access static through the class name). It doesn't matter if you generate an external class object)
Example 4: Private Inner class
| 123456789101112131415161718192021222324 |
class Out { privateint age = 12; privateclass In { publicvoid print() { System.out.println(age); } } publicvoid outPrint() { newIn().print(); }}public class Demo { publicstatic void main(String[] args) { //此方法无效 /* Out.In in = new Out().new In(); in.print(); */ Out out =new Out(); out.outPrint(); }} |
Execution Result: A
Suppose an inner class simply wants to be manipulated by a method in an external class. Then you can declare the inner class using private
In the code above. We have to create an in class object inside the out class to operate on. You can no longer use out.in in = new Out (). New in () to generate an object of the inner class
Other words. The inner class at this point only has an external class to control
As is, my heart can only be controlled by my body. Other people can't access it directly.
Example 5: Method Inner class
| 1234567891011121314151617181920 |
class Out { privateint age = 12; publicvoid Print(finalint x) { classIn { publicvoid inPrint() { System.out.println(x); System.out.println(age); } } newIn().inPrint(); }}public class Demo { publicstatic void main(String[] args) { Out out =new Out(); out.Print(3); }} |
Execution 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
Assuming that we need to pass in a parameter in the method of the outer class, the method shape of the outer classes must be defined with the final
As for final, there is no special meaning here, just a form of representation.
Summary of usage of Java internal classes