Today, I will introduce four internal classes of Java: ordinary internal class, static internal class, local internal class and anonymous internal class.
Tip: the following is the main content of this article. The following cases can be used for reference
1, What is the inner class?-
When the definition of a class appears in the class body of another class, the class is called Inner, and the class in which the Inner class is located is called Outer.
-
When the value of a class is only to serve a class separately, the class can be defined as the internal class in the served class, which can hide the implementation details of the class and easily access the private members of the external class without providing public get and set methods.
2.1 general internal class
Ordinary inner class - directly put the definition of one class in the class body of another class. (object level)
In fact, the writing of this internal class is basically the same as that of the external class.
Features of common internal classes:
- Keywords that can be used for ordinary internal classes are: public, private, protected, final, abstract
- Like ordinary classes, ordinary inner classes can define member variables, member methods, construction methods, etc.
- Ordinary inner classes need to use outer class objects to create objects.
- If the inner class accesses a member variable or method with the same name as the inner class in the outer class, you need to use the this keyword.
Syntax format:
Access modifier class Class name of external class{ Access modifier class The class name of the inner class{ Class body of inner class; } }
Case:
package com.lagou.task03.generalClass; /** * @author Yunmeng GUIYAO * @date 2021/11/22 16:18 * @description */ public class GeneralClass { private int cnt; public GeneralClass() { } public GeneralClass(int cnt) { setCnt(cnt); } public int getCnt() { return cnt; } public void setCnt(int cnt) { this.cnt = cnt; } public void show() { System.out.println("Outer class: " + getCnt()); } // Create the first internal class: normal internal class public class InnerClass { private int cnt; public InnerClass() { } public InnerClass(int cnt) { setCnt(cnt); } public int getCnt() { return cnt; } public void setCnt(int cnt) { this.cnt = cnt; } public void show() { // When the internal class and external class member variable names are the same // The above three methods are all methods to obtain internal class member variables // The last method is to get external class member variables System.out.println("Inner class: " + getCnt()); System.out.println("Inner class: " + this.getCnt()); System.out.println("Inner class: " + InnerClass.this.getCnt()); System.out.println("Outer class: " + GeneralClass.this.getCnt()); // Use this keyword } } }
Corresponding test class:
package com.lagou.task03.generalClass; /** * @author Yunmeng GUIYAO * @date 2021/11/22 16:33 * @description */ public class GeneralClassTest { public static void main(String[] args) { GeneralClass generalClass = new GeneralClass(3); // Instantiate an internal class object using an external class object GeneralClass.InnerClass innerClass = generalClass.new InnerClass(33); generalClass.show(); // Outer class: 3 innerClass.show(); // Inner class: 33 //Inner class: 33 //Inner class: 33 //Outer class: 3 } }
Operation results:
2.2 static internal class
Static inner class - an inner class decorated with the static keyword, belonging to the class level. (class level)
Syntax format:
Access modifier class Class name of external class{ Access modifier static class The class name of the inner class{ Class body of inner class; } }
Features of static internal classes:
- Static inner classes cannot directly access non static members of external classes.
- Static inner classes can create objects directly.
- If a static internal class accesses a member variable or method with the same name in the external class, it needs to use the class name.
Static internal class calls member variables in external class:
- The first way is to write the type of static external class as static, and then use the form of class name. In the static internal column
- The second way is to create a new object in a static inner class and then reference it
Case:
package com.lagou.task03.staticClass; /** * @author Yunmeng GUIYAO * @date 2021/11/22 16:41 * @description */ public class StaticClass { private static int cnt; private int cnt2; public StaticClass() { } public StaticClass(int cnt) { setCnt(cnt); } public static int getCnt() { return cnt; } public void setCnt(int cnt) { this.cnt = cnt; } public int getCnt2() { return cnt2; } public void setCnt2(int cnt2) { this.cnt2 = cnt2; } public void show() { System.out.println("Outer class: " + getCnt()); } // Create a second inner class: a static inner class public static class InnerClass { private int cnt; public InnerClass() { } public InnerClass(int cnt) { setCnt(cnt); } public int getCnt() { return cnt; } public void setCnt(int cnt) { this.cnt = cnt; } public void show() { // When the internal class and external class member variable names are the same // The above three methods are all methods to obtain internal class member variables System.out.println("Inner class: " + getCnt()); System.out.println("Inner class: " + this.getCnt()); System.out.println("Inner class: " + InnerClass.this.getCnt()); // The last method is to obtain external class member variables, // 1. in static internal class, external variables must be static. System.out.println("Outer class: Static member variable " + StaticClass.getCnt()); // 2. In the static inner class, new the object of the outer class, and then reference it StaticClass staticClass = new StaticClass(); staticClass.setCnt2(555); System.out.println("Outer class: Non static member variable " + staticClass.getCnt2()); } } }
Corresponding test code:
package com.lagou.task03.staticClass; /** * @author Yunmeng GUIYAO * @date 2021/11/22 16:49 * @description */ public class StaticClassTest { public static void main(String[] args) { StaticClass staticClass = new StaticClass(5); StaticClass.InnerClass innerClass = new StaticClass.InnerClass(55); staticClass.show(); // Outer class: 5 innerClass.show(); // Inner class: 55 //Inner class: 55 //Inner class: 55 //Outer class: static member variable 5 //Outer class: non static member variable 555 } }
Operation results:
2.3 local internal class
Local inner class - when a class definition is placed directly inside the method body. (method block level)
Syntax format:
Access modifier class Class name of external class{ Access modifier return value type member method name (formal parameter list){ class The class name of the inner class{ Class body of inner class; } } }
Characteristics of local internal classes:
- Local inner classes can only be used inside the method.
- Local inner classes can create objects directly inside methods.
- Local inner classes cannot use access control characters and static keyword modifiers
- Local inner classes can use local variables of external methods, but they must be final. It is caused by different declaration cycles of local internal classes and local variables.
Case:
package com.lagou.task03.areaClass; import javax.swing.*; import java.awt.geom.Area; /** * @author Yunmeng GUIYAO * @date 2021/11/22 16:58 * @description */ public class AreaClass { // If the internal class needs to use the member variables of the external class, in order to ensure data consistency, // Set the external member variable to final private final int cnt; public AreaClass() { cnt = 66; } public int getCnt() { return cnt; } public void show() { System.out.println("Outer class: " + getCnt()); // Local inner classes do not need and cannot be decorated with access control characters and static class InnerClass { private int cnt; public InnerClass() { } public InnerClass(int cnt) { this.cnt = cnt; } public int getCnt() { return cnt; } public void setCnt(int cnt) { this.cnt = cnt; } public void show() { System.out.println("Inner class: " + getCnt()); } } InnerClass innerClass = new InnerClass(666); innerClass.show(); } }
Corresponding test class:
package com.lagou.task03.areaClass; /** * @author Yunmeng GUIYAO * @date 2021/11/22 17:06 * @description */ public class AreaClassTest { public static void main(String[] args) { AreaClass areaClass = new AreaClass(); areaClass.show(); // Outer class: 66 //Inner class: 666 } }
Operation results:
2.4 anonymous inner class (most commonly used)
Anonymous inner class - an inner class without a name.
Syntax format:
- Conventional writing:
Interface/The parent type references the variable name= new Interface/Parent class type() { Method override};
- lambda writing method (features after Java 8)
(parameter list) -> (Method body)
Case:
package com.lagou.task03.anonymousClass; /** * @author Yunmeng GUIYAO * @date 2021/11/22 17:39 * @description */ public interface AnonymousInterface { public void show(); }
Corresponding test class:
package com.lagou.task03.anonymousClass; /** * @author Yunmeng GUIYAO * @date 2021/11/22 17:42 * @description */ public class AnonymousInterfaceTest { public static void main(String[] args) { // The first: anonymous inner class, conventional writing method AnonymousInterface anonymousInterface = new AnonymousInterface() { @Override public void show() { System.out.println("This is the normal way to write anonymous inner classes"); } }; anonymousInterface.show(); // This is the normal way to write anonymous inner classes // The second: anonymous inner class, written in lambda, which is simpler (Java 8) AnonymousInterface anonymousInterface1 = () -> System.out.println("This is an anonymous inner class lambda Writing method"); anonymousInterface1.show(); // This is an anonymous inner class lambda } }3, Summary
The above are the implementation methods of the four internal classes in Java. Relatively speaking, anonymous internal classes will be more commonly used, and if anonymous internal classes are written in lambda, they will be more concise.
Well, that's all for this issue. Thank you.