In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what internal classes and internal interfaces Java has". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what internal classes and internal interfaces Java has.
one。 Inner class
An inner class defines a class in a class. We can think of a class in a class as a property of the class, which can be static or non-static. Internal classes can also be defined in the methods of the class, plus anonymous classes, there are a total of 5 internal classes.
1. Static inner class
The static inner class defines a class of static inside the class, as shown in the following code:
Public class StaticInnerClass {
Static class Inner {
Void print () {
Log.info ("Inner class is:" + this)
}
}
Public static void main (String [] args) {
StaticInnerClass.Inner inner = new StaticInnerClass.Inner ()
Inner.print ()
}
}
Note: in the static inner class, because the static variable can be accessed directly based on the class name, we use new StaticInnerClass.Inner () to instantiate the inner class.
two。 Non-static inner class
Non-static inner classes can also be non-static even classes defined in class, as shown in the following code:
Public class InnerClass {
Class Inner {
Void print () {
Log.info ("Inner class is:" + this)
}
}
Public static void main (String [] args) {
InnerClass.Inner inner = new InnerClass () .new Inner ()
Inner.print ()
}
}
Note: for a non-static inner class to access a class's variables, you need to instantiate the outer inner class, and then instantiate the inner class: new InnerClass (). New Inner (). (note: two new are required)
3. Static method inner class
The inner class of a static method is to define a class in a static method, which is actually equivalent to a variable in the method, which, of course, cannot be static. The following code is shown:
Public class StaticMethodInnerClass {
Private static String x = "static x"
Public static void print () {
Class MyInner {
Public void printOuter () {
Log.info ("x is" + x)
}
}
MyInner I = new MyInner ()
I.printOuter ()
}
Public static void main (String [] args) {
StaticMethodInnerClass.print ()
}
}
Note: classes in methods cannot be instantiated externally.
4. Inner class of non-static method
The same non-static method can also define an inner class, as shown in the following code:
Public class MethodInnerClass {
Private String x = "non static x"
Public void print () {
Class MyInner {
Public void printOuter () {
Log.info ("x is" + x)
}
}
MyInner I = new MyInner ()
I.printOuter ()
}
Public static void main (String [] args) {
New MethodInnerClass () .print ()
}
}
Note: here you need to instantiate the external class before you can continue to call.
5. Anonymous inner class
Anonymous classes are classes that are instantiated directly when needed. Anonymous classes are common, for example, when building SortedSet, you can pass in a custom Comparator, we can implement it with anonymous classes, or you can use lambda expressions directly. The following code is shown:
Public class AnonymousClass {
Public static void main (String [] args) {
SortedSet sortedSet1 = new ConcurrentSkipListSet (new Comparator () {
@ Override
Public int compare (Object o1, Object O2) {
Return 0
}
});
SortedSet sortedSet2 = new ConcurrentSkipListSet ((o1, O2)-> 0)
}
}
two。 Internal interface
Inner Interface refers to the interface defined in the interface. Our most common is the Entry in Map:
Public interface Map {
Interface Entry {
K getKey ()
}}
The inner interface here must be static, because the interface cannot be instantiated, so in order to access the interface in the interface, it must be defined as static. If not specified, the default is static.
Let's take a look at an implementation of this inner interface, as shown in the following code:
Public class MapImpl implements Map.Entry {
@ Override
Public Object getKey () {
Return 0
}
@ Override
Public Object getValue () {
Return null
}
@ Override
Public Object setValue (Object value) {
Return null
}
} Thank you for your reading. the above is the content of "what internal classes and internal interfaces does Java have?" after the study of this article, I believe you have a deeper understanding of the problem of what internal classes and internal interfaces Java has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.