Java-Preliminary Understanding-tenth-Anonymous internal Classes-Overview

Source: Internet
Author: User

One.

Anonymous inner class, anonymous format for inner class. Anonymous, previously studied, is an anonymous object. An anonymous object is a shorthand format for an object. The same is true for anonymous classes here.

Shorthand is for convenience, so is the ternary operator. Just started to write anonymous inner Class A little twist, write more and get used to it.

The shorthand for anonymous inner classes has a premise, not all internal classes can be abbreviated.

Abbreviations must have a premise. The IF Esle variable shorthand must have the result of the operation.

The premise of anonymity of the inner class indicates that the inner class must be related to the outside, no matter what. ( can an external class with a relationship here be an external class to which the inner class belongs ?) The other classes are implemented in the following example. Should not be the inner class belongs to the external class, because such inheritance or implementation, there is a loop nested meaning, and inherited the outer class inside the inner class, this is my personal understanding. )

First write a common inner class,

(where the inner class is located at the member location of the outer class, and the member function wants to access the inner class, the object of the class must be created, because it is the Access object, actually the method in the calling class, and if it is not the contents of the calling class, how can it be a class?) Here is simply the member function that wants to access the class,)

Is the most common form of internal class program writing. In method () methods, the inner class object that was created is called only once, followed by the rules of the previous anonymous object. This can be written in the form of an anonymous object, which is only anonymous for the creation of the object, not the inner class. One is anonymous for the object being created, and the other is anonymous for the class, not the same.

The video shows here that the anonymous class means that the class has no name, as follows, so it's bad (how can a class have no name?). )

The inner class has no class name, so what is new? This is the tangled place, (how do you understand that?) What do anonymous classes mean without a class name? Before the anonymous object, there is no object name, here is said there is no class name? Is it possible to have a class name in the correct way? )

If you want to implement an inner class without a name and make an anonymous class, you must follow the premise .

Formally according to the inheritance, it should be as shown, just here the inner class name is Outer$inner, and after inheritance, the demo show () method to make a copy, and then in the next member function to create an anonymous object, while calling the method, which is no problem.

The inner class now achieves the premise of an anonymous inner class, and if it is replaced with an unnamed one, how to write it.

The new inner is written in the member function, because it involves the name of the inner class.

According to the written, there is no name. Inner No, now only new his father, if not even the premise, then I new who? Did you get a new demo on it?

An accident, the demo is abstract, do not write abstract there is no problem? Still didn't catch the core. There are two parts, one is the new part, the other is the part of the call. If you make a duplicate of an abstract object, you can create an object. Does the method have to be encapsulated? Yes, you cannot write a semicolon, write a semicolon is an object created.

After writing the curly braces, this is called new a subclass object of the demo class, because only subclasses have the right to overwrite the show method, and I define the overridden method in this interval. With the object of the content,

This is the anonymous inner class. (Knowledge of anonymous inner classes here: one is that the inner class does not have a name, and the other is that the inner class is positioned locally, not the member position)

In fact, is the new object, the object also carries the content, the object can take the content of the demo sub-class, because only the subclass can overwrite the method,

According to the argument, is it still a class, is it not the object of the subclass?

Anonymous not anonymous, regardless, is actually a subclass object, it is to do a simplified encapsulation action. Supposedly, we want to make a class to inherit its father, overwrite its method, and then the member function in the new object, call the method. Now it's one go, even with the replication and the new object,

The format of the specific anonymous inner class is as follows

It is also possible to extend the program to the anonymous inner class, which adds a haha method, in addition to the inherited methods in the subclass object, and its own unique method, which is no problem.

These are a shorthand format, looking at some twist, but it's actually a reflection,

looking back, after the anonymous internal class format has been written, we implement the subclass inherits the method of the replication, the new object, another call method, followed by the supplementary call.

The following is how to implement such a program in the main function. Why is there such a doubt here? or the use of internal classes is not familiar .

The blue bottom part of the following is the anonymous inner class,

Always keep in mind that this is the shorthand format for the inner class, omitting the class name, so it is called an anonymous inner class. It is the same as by malpractice.

To tell the truth, but looking at this form, it is indeed a shorthand for a subclass object that is distributed within the class, simplifying the subclasses that are located inside.

Java-Preliminary Understanding-tenth-Anonymous internal Classes-Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.