Is the inner class of Java really so difficult to understand?

I parked my car and went home last night. When I got home, I found my cell phone in the car, but it was too cold outside, It's so cold that I can feel the chill in my bones - I really don't want to go back to pick it up (the security of the community is good, so I don't have to worry about being smashed by car windows), so I decided to live in a "paradise" for a few hours - others can't find me, and I can't find others. This isolated state is very suitable for reading and writing. Put the thick Java programming thought He sat on the table and read Chapter 10 - internal class seriously. Although I have been very patient and attentive, this chapter of the internal class is very boring and difficult to understand. My whole person is almost on the verge of collapse. A long time ago, a friend who wanted to change his career to learn java asked me what kind of books I could recommend. I solemnly introduced Java programming ideas, and repeatedly told him that this is an entry-level classic book of Java, and he must be patient to read it. Now think about it, my recommendation at that time was really rash! I say this not to deny the value of the Book Java programming ideas, because from the perspective of books, it may say with emotion: there is a problem with Wang Er's learning ability. It is so difficult for me to read! There is a saying: "if you have a hammer in your hand, everything looks like nails." I think the chapter "internal class" is difficult to understand. The fundamental reason is that I don't have a good understanding of "internal class". If you want to continue to have a solid basic knowledge of Java, the only thing to do is to try every means to understand the "internal classes" and sort them out.

As the name suggests, an inner class is a class defined inside another class. It is very important that the inner class can access all members of the outer class, including those decorated with private. Look at program listing 1-1: now();  Now(); What does} program listing 1-1 mean? The answer is: I, silent King II, am 29 years old, Born in 1989 (some people say that I was born in 1989, but I am 30 years old). When I am old, I always want to be young and understand. I don't read much, but I love thinking, so I created an internal class thought for myself. As can be seen from program listing 1-1, although thought is an internal class, I can access the private member variable age of the external class Wanger. If I want to create an internal class The type of object reference needs to be specified first. The format is outerclassname Innerclassname, like Wanger. In the main () method Think like that. Next, we will create internal class objects in two forms. The first form is to define a method thought getthought() in the external class to return the internal class object created with the new keyword, and then use the external class object to call the method Wanger getThought(); The second form is directly through external class objects New create Wanger new Thought()。

From my programming experience, anonymous inner classes are most frequently used when creating threads. Look at program listing 2-1: meta "> @ override. In program listing 2-1, there is a thread object thread inside the test () method, which is created through new thread(). New thread() can receive an object that implements the runnable interface type. How can this object be created? It can be created in the form of an anonymous internal class - New runnable() {public void run(){......}}—— This short code is equivalent to: meta "> @ override @ anonymous inner class. The advantage is that it not only saves the process of defining the implementation class, but also can automatically transform upward. In program listing 2-1, test () Method also has a parameter title, jdk1 Before 8, the compiler required it to be of type final. But jdk1 After 8, if we need to access a local variable in an anonymous inner class, the local variable no longer needs to be modified with the final keyword. But if you want to change the value of a local variable in an anonymous inner class, the compiler will remind you not to do so. It will prompt: "the local variable defined in a closed range must be final."

Java's inner class makes it easy for me to remember JavaScript closures. Closures are functions defined inside a function -- it sounds the same as Java's inner class definition. In essence, closure is a bridge connecting the inside and outside of a function. Like the inner class, it is a bridge between the inner class and the outer class. Let's take a look at what is a closure: in addition, the most noticeable reason for the internal class is that the internal class can independently inherit an abstract class or implement an interface. Whether the external class does the same or not, it has no impact on the internal class. In short, internal classes make the content of classes rich and colorful, just as one person can live in another person's heart.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>