The inner class is also a kind of encapsulation protection, that is, the structure of the class is nested in another class.
Internal classes in java mainly include the following four types: member internal classes, static internal classes, method internal classes and anonymous internal classes
catalogue
2. Usage rules of member internal classes
3. Creation of class objects within members:
4. Internal classes and static fields
2. Usage rules of static internal classes:
3. Creation of static internal class objects
Member inner class VS static inner class
2. Usage rules of method internal classes:
3. Pay attention to the use of formal parameters
Anonymous inner class (functional programming)
Member inner class
1. Definitions
Member inner class is an inner class directly defined in the class without any modifier (especially without static modification). It can be understood by analogy with member variables. For example, the Inn class in the following code segment is a member inner class
public class Inner1 { private String o = "External class properties"; public class Inn{ private String i = "Member internal class properties"; } }
2. Usage rules of member internal classes
- The internal class can directly access the member variables of the external class, but because the internal class is also a class, the external class must access the member variables of the internal class through the objects of the internal class
- The internal class is equivalent to the member variable of the external class, so the internal class and the external class can easily access each other, including the attributes and methods modified by privte permission
- Member internal classes can achieve "curve saving the country", inherit other classes through multiple internal classes, and realize multi inheritance in a disguised form
//Member inner class public class Inner1 { // Private properties of external classes o private String o = "External class properties"; public class Inn{ // Private property of inner class i private String i = "Member internal class properties"; private void Itest(){ // The inner class directly accesses the properties of the outer class, including private properties System.out.println(o); } } public void Otest(){ // For an external class, you must first create an object of the internal class and then access its member variables. Note here that you can also access its private properties Inn in = new Inn(); in.Itest(); System.out.println(in.i); } public static void main(String[] args) { Inner1 i1 = new Inner1(); i1.Otest(); } }
3. Creation of class objects within members:
- Internal creation of external classes: the same as the object creation method of normal classes,
- Internal class name object name = new class name ()
public class Inner1 { private String str1 = "External class"; // Create an inner class object inside an outer class Inn i1 = new Inn(); private class Inn{ private String str2 = "Inner class"; } }
- External creation of external class: the internal class of a member is equivalent to a special member variable of the external class. Therefore, we know that the internal class of a member depends on the existence of the external class, that is, there must be an object of the external class before the object of the internal class of the member can be created
- External class name Internal class name object name = new external class name () New inner class name ()
- At present, when creating an internal class object outside an external class, the premise is that the internal class is visible outside the external class
As shown in the following figure, the internal class is private, and the object cannot be created outside the external class
Correct creation when modifying the internal class access permission to public:
public class Inner1 { private String str1 = "External class"; // Create an inner class object inside an outer class Inn i1 = new Inn(); public class Inn{ private String str2 = "Inner class"; } } class Test{ // External class creates an internal class object of an external class Inner1.Inn i2 = new Inner1().new Inn(); }
4. Internal classes and static fields
The internal class of a member can access the static domain of the external class, but it cannot define static variables by itself (the internal class of a member depends on the external class. If it can define static variables by itself, it can be accessed without external objects, which loses its meaning)
Static methods in external classes can not use members' internal classes, which is equivalent to calling member variables in static variables.
For this reason, pay special attention to that the main method is a static method, so you cannot directly create an internal class object in the main method of an external class. You can only create it with the help of an external class
public class Inner1 { private String str1 = "External class"; // Create an inner class object inside an outer class Inn i1 = new Inn(); public class Inn{ private String str2 = "Inner class"; } public static void main(String[] args) { // Create in main Inner1.Inn inn = new Inner1().new Inn(); System.out.println(inn.str2); } }
Static inner class:
1. Definition:
- Static internal classes are also defined in classes, but different from member internal classes, they are decorated with static. They are static internal classes, which can be understood by analogy with static variables
- static modification, so it does not need to rely on external class objects. We can regard it as a common class
public class Inner2 { private String str1 = new String("external"); // Static inner class public static class Inn{ private String str2 = new String("Static internal"); } }
2. Usage rules of static internal classes:
- Static internal classes cannot directly access ordinary member variables of external classes (ordinary member variables can only be accessed by objects, so static cannot access ordinary member variables). Of course, the purpose of accessing external class member variables can be achieved by creating an object of an external class
public class Inner2 { private String str1 = new String("external"); private static String str3 = new String("External static properties"); // Static inner class public static class Inn { private String str2 = new String("Static internal"); public void fun(){ // Static inner classes cannot directly access ordinary member variables of outer classes System.out.println(str1); // But it can be accessed through new an external class object Inner2 i2 = new Inner2(); System.out.println(i2.str1); // static family interworking System.out.println(str3); } } }
- However, although the static internal class cannot directly access the member variables of the external class, it can have its own ordinary member variables
3. Creation of static internal class objects
- Internal of external class: the usage is the same as that of ordinary classes and different from that of member internal classes. In static internal classes, objects of static internal classes can be created in the static methods of external classes (static family mutual access). Of course, objects without objects can be created, so static internal class objects can also be created in the member methods of external classes
- Internal class name object name = new class name ()
public class Inner2 { private String str1 = new String("external"); // Static inner class public static class Inn{ private String str2 = new String("Static internal"); } // Create static objects directly inside the class Inn i = new Inn(); public static void main(String[] args) { // Create an object that uses a static inner class in a static method of an outer class Inn inn = new Inn(); } }
External of external class:
External class name Internal class name object name = new external class name Internal class name ()
public class Inner2 { private String str1 = new String("external"); // Static inner class public static class Inn { private String str2 = new String("Static internal"); } } class Test{ // An object that creates a static inner class outside the outer class Inner2.Inn i = new Inner2.Inn(); }
Member inner class VS static inner class
- The inner class of a member can access the member domain and static domain of the outer class, but cannot have its own static domain
- Static internal classes cannot directly access the member domain of external classes, but they can have their own member domain. In addition, static families can visit each other at will
Method inner class:
1. Definition:
It is directly defined inside the method. It is not allowed to use any modifiers (including access permission modifiers, static, etc.) to modify it. It is completely hidden from the outside. Without this method, this class is not available. It can be understood by analogy with local variables
2. Usage rules of method internal classes:
Except that the static domain cannot be defined, it is basically the same as the inner class of the member, and will not be repeated here
3. Pay attention to the use of formal parameters
If a method parameter is used in the internal class of the method, it is an implicit final declaration (before JDK8, it must be explicitly defined as final manually, but after JDK8, it must be directly and implicitly final)
For example:
public class Inner3 { public void fun(int num){ // Method inner class class Inn{ public void test(){ // Comment out the following line of code. num can be operated normally // System.out.println(num); } } num ++; System.out.println(num); } public static void main(String[] args) { Inner3 i3 = new Inner3(); i3.fun(3); } }
However, if our method's internal class uses the method's formal parameters, the compilation will report an error
Anonymous inner class (functional programming)
- It is a special version of the internal class of the method, and the class name is not written directly
- Comply with all requirements of the inner class of the method
- By default, it will inherit a class or implement an interface. Inherited classes are generally inherited abstract classes
- This class is usually used in the process of parameter passing, which can simplify the writing method
As we wrote earlier, the interface parameters are:
public class Inner4 { public static void fun(MessageImpl m) { m.printMessage(); } public static void main(String[] args) { MessageImpl l = new MessageImpl(); fun(l); } } interface IMessage { void printMessage(); } class MessageImpl implements IMessage { public void printMessage() { System.out.println("aaa"); } }
The anonymous inner class can be written as follows:
public class Inner4 { public static void fun(IMessage m ){ m.printMessage(); } public static void main(String[] args) { // Anonymous Inner Class fun(new IMessage() { @Override public void printMessage() { System.out.println("aaa"); } }); } } interface IMessage { void printMessage(); }
Finally, focus on mastering member internal classes and static internal classes, as well as the differences between them.
For method inner classes, especially anonymous inner classes, you can understand them.