In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The content of this article mainly focuses on how to learn about the internal classes in Java. The content of the article is clear and well-organized. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Read a lot of source code, are useful to the internal class, but in the past in the production environment, do not use, but also used but very little, so today I intend to go through it from beginning to end.
Define
You can put the definition of one class inside another class, which is the inner class. The so-called inner class concept only appears in the compilation phase, and there is no inner class concept for the jvm layer. We can use inner classes to solve the problem
The problem of single inheritance of a class. Classes that can no longer be inherited by an external class can be inherited by an inner class.
We can implement a class that belongs to a class by defining an inner class to achieve better encapsulation.
Code optimization: it requires less code
classification
Inner classes can be divided into:
Static inner class.
Non-static inner class.
Non-static inner classes can be divided into:
Member inner class.
Method inner class.
Anonymous inner class.
Static inner class
I think this is the most frequently used, for example, the design of Redis's key, because we need to concatenate: numbers in the middle, so it is very good to use static inner classes to form different key, so that the same type of key can be in the same file directory.
Static inner class definition and ordinary static variables or static methods of the definition method is the same, using the static keyword, but this time static is modified on class, generally speaking, only static internal class is allowed to use static keyword modification, ordinary class definition can not be modified with the static keyword, this should be noted.
The following defines a static inner class:
Public class Out {private static String name; private int age; public static class In {private int age; public void sayHello () {System.out.println ("my name is:" + name); / /-compile error-/ / System.out.println ("my age is:" + age);}
In the above code, the In class is a static inner class. We say that the inner class can access the private fields and private methods of the external class, and for the static inner class, it follows a consistent principle and can only access the static members of the external class.
In the above code, the non-static private field age of the external class is not allowed to be accessed in the static inner class, while the static field name is accessible. Let's see how to create an instance object of a static inner class.
Public static void main (String [] args) {Out.In innerClass = new Out.In (); innerClass.sayHello ();}
Using scenarios, in general, you can consider defining a static inner class in cases that are closely related to an external class but do not depend on an external class instance. Let's take a look at the slightly more complex inner classes of members.
Member inner class
As we said, each of the four different types of inner classes has its own usage scenarios, and static inner classes are suitable for situations that are closely related to external classes but do not rely on external class instances. However, in cases where you need to be associated with an external class instance, you can choose to define the inner class as the member inner class.
The following code defines a simple member inner class:
Public class Out {private String name; public void showName () {System.out.println ("my name is:" + name);} public class In {public void sayHello () {System.out.println (name); Out.this.showName ();}
A simple inner class In is defined above. Our member inner class can directly access the member fields and member methods of the external class because it is associated with an instance of the external class. Let's take a look at how the inner class instance is created externally.
Public static void main (String [] args) {Out out = new Out (); out.setName ("six veins Shenjian") Out.In in = out.new In (); in.sayHello ();}
Because a member's inner class is associated with a specific external class instance, its instance creation must be created by an external class instance.
For instance creation, we just need to remember that the instance creation of the member internal class needs to be associated with the external class instance object, while the static internal class instance creation is relatively simple. Let's take a look at how the compiler keeps the inner class accessible to the external class member information during the compilation phase.
Using scenarios, it is more sensible to define an inner class of a member in cases where you are highly dependent on external class instances.
Method inner class
The inner class of a method, as its name implies, is defined within a method. The inner class of a method is relatively more complex, so let's define a method inner class:
Public class Out {private String name; public void sayHello () {class In {public void showName () {System.out.println ("my name is:" + name);}} In in = new In (); in.showName ();}}
We define a class, in which we define a method sayHello, but in this method we define an inner class, and the class In is a method inner class. The life cycle of our method inner class does not exceed the life cycle of the method that contains it, that is, the method inner class can only be used in the method. So any access modifiers are meaningless at the time of declaration, so Java simply does not allow any access modifiers to modify the inner class of the method.
It is also important to note that definition and use are two different things, regardless of the long string of code that defines the class, you actually want to use the class, you must new the object, and for the inner class of the method, you can only new the object inside the method. This is a brief introduction to the inner class of the method, and let's take a look at how it is implemented.
In fact, the implementation principle of the inner class of the method is not much different from that of the member inner class, and an external class instance is passed into the inner class when it is initialized. What is the difference? The reason is that the inner class of the method is defined inside the concrete method, so the class can access the fields and methods in the external class through the passed external instance, and the parameters passed in the method containing it are also initialized to the inner class along with the external class instance.
There is no doubt that the encapsulation of the inner class of the method is better than the two previously introduced. Therefore, a class is generally defined as a method inner class only when a high degree of encapsulation is required.
Anonymous inner class
Of all the categories of inner classes, the name of anonymous inner class is the largest, and it is also the most commonly used, mostly in functional programming, lambda expressions and so on. Let's focus on this anonymous inner class.
An anonymous inner class is an inner class without a name. When the definition is complete, the instance is also created, often closely associated with the new keyword. Of course, it is not limited to a class, it can also be an interface, and it can appear anywhere.
Let's define an anonymous inner class:
If you must override the method of a class or interface, you should use it. You can create Java anonymous inner classes in two ways
/ / first define a normal class public class Out {private String name; public void sayHello () {System.out.println ("my name is:" + name);}}
~
/ define and use an anonymous inner class public static void main (String [] args) {Out out = new Out () {@ Override public void sayHello () {System.out.println ("my name is cyy");} public void showName () {System.out.println ("hello single");}; out.sayHello () }
It is obvious from the above code that our anonymous inner class must rely on a parent class because it has no name and cannot be represented by a specific type. So anonymous inner classes often implement the definition of an anonymous inner class by inheriting a parent class, rewriting or redeclaring some members. In fact, it still uses the principle of Li-type conversion.
In fact, after looking at the principles of the above three inner classes, I feel that the implementation of anonymous inner classes is relatively simple. The main idea is to extract the internal class and initialize the instances of the external class to achieve the access of all members of the external class. It's just that in an anonymous inner class, the dependent parent class is not its external class.
The main feature of anonymous inner classes is that without a name, the object can only be used once and can appear anywhere. So its usage scenario is also coming out, for some cases where code simplicity is required, anonymous inner classes can be preferred.
Thank you for your reading. I believe you have a certain understanding of "how to learn internal classes in Java". Go and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
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.