OOP of 07_JavaSE -- object-oriented (internal class), 07_javaseoop --
Object (7)
I. Overview of inner class
An internal class, as its name implies, is a class defined in a class. For example:
1 class OuterClass{2 3 class InnerClass{4 5 }6 }
Next, let's talk about why we should use the internal class, what features it has, and how to use it.
1. Why use internal classes?
- Internal class methods can access the scope data of the class definition, including private data.
- Internal classes can be hidden from other classes in the same package.
- If you want to define a callback function and do not want to write a large amount of code, it is easier to use anonymous internal classes. [1]
2. Features of internal class access and how to use it
- The reason for use is actually one of its features:You can access the scope data of the class definition, including private data..
- To access internal class members of an external class, you must create an object.
See the Code:
1 public class Test_InnerClass {2 public static void main (String [] args) {3 // external class name. internal class name object name = External class object. internal class object; 4 OuterClass. innerClass oi = new OuterClass (). new InnerClass (); 5 // call Method 6 oi. method (); 7} 8} 9 10 class OuterClass {11 private int num = 10; 12 class InnerClass {13 public void method () {14 System. out. println (num); // you can directly access the member variable 15 System of the external class. out. println ("thanks for your attention ~ "); 16} 17} 18}
Result:
Ii. private and static member Internal classes)
1. Private
The so-called private class in the member is to add a private class to the internal class to become private. For example:
1 class OuterClass {2 private class InnerClass {3 public void method () {4 System. out. println ("thanks for your attention ~ "); 5} 6} 7}
Add a private keyword before InnerClass. That's right.
So how to use it?
Talk is cheap, show me the code. Come:
1 class OuterClass {2 3 private class InnerClass {// The internal class of the member is private 4 public void method () {5 System. out. println ("thanks for your attention ~ "); 6} 7} 8 9 // instantiate the internal class by writing a method in the external class and calling its method 10 public void invokeMethod () {11 InnerClass innerClass = new InnerClass (); 12 innerClass. method (); 13} 14 15}
If you want to call the class, you can write a method in the external class to instantiate the internal class and call its method.
2. static member internal class
Static member internal class, internal class plus static. For example:
1 public class Test_InnerClass {2 public static void main (String [] args) {3 // external class name. Internal class name object name = External class name. Internal class object; 4OuterClass. InnerClass o = new OuterClass. InnerClass ();5 o. method (); 6} 7} 8 9 class OuterClass {10 static class InnerClass {11 public void method () {12 System. out. println ("Hello World"); 13} 14} 15}
Result:
Visible, throughExternal class name. Internal class name object name = External class name. Internal Class Object.
There may be a bit of confusion here. That is, when calling Internal classes and static internal classes.
Therefore, make a comparison:
Common internal class call |
External class name. Internal class name object name = External Class Object. Internal Class Object
OuterClass. InnerClassOi= New OuterClass (). new InnerClass (); |
Static internal class call |
External class name. Internal class name object name = External class name. Internal Class Object OuterClass.InnerClass o = new OuterClass.InnerClass(); |
For more information about common internal and static internal classes, see. Haha
Next let's take a look at this question: (requires the console to output, 20, 10)
1 class Outer { 2 public int num = 10; 3 class Inner { 4 public int num = 20; 5 public void show() { 6 int num = 30; 7 System.out.println(A); 8 System.out.println(B); 9 System.out.println(C);10 }11 }12 }13 class TestInnerClass{14 public static void main(String[] args) {15 Outer.Inner oi = new Outer().new Inner();16 oi.show();17 }18 }
How do I fill in a B C?
The reason why an internal class can get the members of an external class is that it can get the reference of an external class. External class. this to obtain its value.
3. Local internal class (internal class in the method)
The so-called local internal class is actuallyInternal class in the Method. For example:
1 public class Test_InnerClass {2 public static void main (String [] args) {3 OuterClass outerClass = new OuterClass (); 4 outerClass. method (); 5} 6} 7 8 class OuterClass {9 10 public void method () {11 class InnerClass {// local internal class 12 13 public void playChiJi () {14 System. out. println ("good luck, eat chicken tonight ~ "); 15} 16} 17 // you can only instantiate the method and access the chicken eating Method 18 InnerClass innerClass = new InnerClass (); 19 innerClass. playChiJi (); 20} 21 22/* 23 does not work. Because of the scope problem, the internal class only exists in the method, so InnerClass24 public void invoke () cannot be instantiated in the external class () {25 InnerClass innerClass = new InnerClass (); 26 innerClass. playChiJi (); 27} 28 */29}
Result:
ThatLocal internal classAccessLocal variableWhat about it?
1 public class Test_InnerClass {2 public static void main (String [] args) {3 OuterClass outerClass = new OuterClass (); 4 outerClass. method (); 5} 6} 7 8 class OuterClass {9 public void method () {10 final int num = 10; 11 class InnerClass {// local internal class 12 13 public void playChiJi () {14 System. out. println (num); // local internal class access local member Method 15} 16} 17 // The method can only be instantiated and accessed in the Method 18 InnerClass innerClass = new InnerClass (); 19 innerClass. playChiJi (); 20} 21 22}
Result: Output 10.
Before jdk1.8, final must be added before the local variable, because when this method is called, if the local variable is not modified using final, its lifecycle is the same as that of the method, when the method stacks, this local variable will also disappear, so if the local internal class object does not disappear immediately and you want to use this local variable, there will be no more, if the final modifier is used, it will enter the constant pool when the class is loaded. Even if the method plays the stack, the constant of the constant pool is still in use.In short, the lifecycle of local variables is extended by adding final.Method To continue accessing the stack.
However, jdk1.8 can be used without final. In fact, it is added for you by default.
4. Anonymous internal class (class without name ?)
Yes, it is actually a class without a name. Haha.
In fact, it is a short form of the internal Department class. It is suitable for the situation where only one method is called.
(Non-Anonymous internal class)
1 public class Test_InnerClass {2 public static void main (String [] args) {3 OuterClass outerClass = new OuterClass (); 4 outerClass. method (); 5} 6} 7 8 // define an interface 9 interface NoNameInter {10 void playLOL (); 11} 12 13 class OuterClass {14 15 class InnerClass implements NoNameInter {16 // rewrite Method 17 @ Override18 public void playLOL () {19 System. out. println ("black together at night ~ "); 20} 21} 22 23 // This is a named internal class 24 public void method () {25 InnerClass innerClass = new InnerClass (); 26 innerClass. playLOL (); 27} 28}
View Code
(Anonymous internal class)
1 public class Test_InnerClass {2 public static void main (String [] args) {3 OuterClass outerClass = new OuterClass (); 4 outerClass. method (); 5} 6} 7 8 // define an interface 9 interface NoNameInter {10 void playLOL (); 11} 12 13 class OuterClass {14 15 public void method () {16 17 new NoNameInter () {// anonymous internal class 18 // rewrite method 19 @ Override20 public void playLOL () {21 System. out. println ("black together at night ~ "); 22} 23}. playLOL (); 24} 25 26}
View Code
We need to explain it one by one. First, the internal class is anonymous.Writing formatYes:
New Class Name or Interface Name (){
Rewrite method;
}
Prerequisites:There is a class or interface
The class can be either a specific class or an abstract class, just as the NoNameInter interface in the code.
In essence, it inherits the class or the anonymous object of the subclass that implements the class interface. I want to go. Yes,
This is the writing method and usage of anonymous internal classes.
If any error occurs, please correct it.
Email: it_chang@126.com