In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the concept and classification of internal classes in Java, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
You can only use the modifier qualifier: public and default to modify the class
Inner class concept
In Java, a class is defined inside another class, called an inner class
Is to nest other class structures within one class.
Inner class is also an embodiment of encapsulation.
For example:
/ / external class public class OutClass {/ / inner class class InnerClass {}}
Note: internal and external classes share a java source file, but after compilation, they form separate bytecode files.
Classification of internal classes: internal classes of members
In an external class, the inner class definition is in the same position as the external class member, so it is called the member inner class.
The inner classes of members are: ordinary inner class and static inner class.
Ordinary inner class
Inner classes of members that are not modified by static
/ / external class public class OutClass {public void method () {} / / ordinary inner class class InnerClass1 {}}
How to use ordinary inner classes?
Let's first look at the use of ordinary classes:
Package Date20210917;public class Test1 {int a; public void method1 () {} / / ordinary inner class class InnerClass {int b; void method2 () {}} public static void main (String [] args) {Test1 test1 = new Test1 (); test1.method1 ();}
Normal class: construct the object first-access the members within the class through the object
So, we can try to new an InnerClass object:
We find that it is not feasible, because InnerClass is also a member of the outer class, so when we use the inner class, we need to use the object of the outer class to create the object of the inner class.
Then the correct operation is:
Public static void main (String [] args) {/ / normal class: first construct the object-access the member inside the class through the object Test1 test1 = new Test1 (); test1.method1 (); / / ordinary inner class InnerClass innerClass = test1.new InnerClass (); innerClass.method2 ();}
The use of ordinary inner classes under different files:
Package Date20210917;// external class public class OutClass {/ / Test the use of the inner class in Test1: void test () {Test1 test1 = new Test1 (); / / the object to instantiate the inner class in Test1 / / InnerClass; compiler is not recognized because the class is not in this file Test1.InnerClass t = test1.new InnerClass ();} public static void main (String [] args) {}}
When there is a variable with the same name in the internal and external class, use the nearest principle ~ that is, give priority to the
Public class Test1 {int a; int b; public void method1 () {} / / ordinary inner class class InnerClass {int b; void method2 () {a = 10; method1 (); b = 66; / / assign a value} to the member variable b of the inner class itself
Assign values to member variables of the external class with the same name in the inner class:
Test1.this.b = 88
Debug verification:
That is, after compilation, make the following changes to method2
Before modification:
Void method2 () {a = 10; method1 (); b = 66; / / assign the member variable b of the inner class to Test1.this.b = 88;}
After modification:
Void method2 (InnerClass this) {this$0.a = 10; method1 (this$0); this.b = 66; Test1.this.b = 88;}
Summary:
An ordinary inner class object can only be created if there is an external class object first.
In an external class, you cannot directly access the members of the inner class. If you want to access the objects of the inner class, you must first create the objects of the inner class.
Any member of an external class can be accessed directly in an ordinary inner class method
When accessing a member with the same name in an inner class method, you have priority to access your own (nearest principle). If you want to access a member of an external class with the same name, you must: external class name. This. Members of the same name to visit
Static inner class
Inner member classes modified by static are called static inner classes.
Public class Test2 {int a; int b; static int c; static void method1 () {System.out.println ("I am method ()");} static class InnerClass {int d; static int e; void method2 () {}
Creation of static inner class object
1. Create in the same file:
/ / access System.out.println (Test2.c) of static member variables; / / create InnerClass innerClass = new InnerClass () without using external class objects
two。 Create in different files:
Void test2 () {/ / static member variable access: System.out.println (Test2.c); / / static inner class object (analogous to static member variable access) Test2.InnerClass t = new Test2.InnerClass (); / / static inner class object can be created directly without relying on external class object}
Can objects of external classes be accessed in static inner class methods?
As can be seen from the above figure: in a static inner class, you can only access static member variables and static member methods in an external class.
Note:
Only static members in an external class can be accessed in an inner class
When you create internal class objects in the same file, you can create them directly without the need for external class objects.
When creating inner class objects in different files, you can also directly create: file name. Inner class name. Object name
Member inner class, compiled to generate a separate bytecode file, named in the format: external class name $internal class name
Local inner class
Defined in the method body or {} of an external class, this inner class can only be used in its defined location and is rarely used
/ / external class public class OutClass {public void method () {/ / partial inner class class InnerClass3 {}} {/ / partial inner class class InnerClass4 {}}
Cannot be modified by access modifier qualifier:
Static members can no longer be defined in local inner classes
Static methods can no longer be defined in local inner classes
Note:
The local inner class can only be used inside the defined method body and cannot be modified by modifiers such as public, static, etc. The compiler also has its own independent bytecode file, naming format: external class name $x inner class name .class, x is an integer anonymous inner class (not explained here)
After reading the above, do you have any further understanding of the concept and classification of inner classes in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.