Java-Preliminary Understanding-tenth-Anonymous inner class-application

Source: Internet
Author: User

One.

The public of the method in the inner inner class must carry, or the compilation will fail? are the permissions of the subclass greater than the permissions of the parent class ?

The above is a common form. Next, simplify the anonymous inner class.

According to the anonymous inner class that we discussed earlier, it is difficult to write in multiple methods.

What do we do now to make multiple method calls to an object? Just name it and borrow your previous experience.

Give it a name, by the type of the parent class, not the subclass or the inner class, which is still an anonymous inner class. This is also the use of polymorphism.

This is what we call the basic application of anonymous internal classes.

The application scenario for anonymous inner classes is this:

Not more than three, is said one or two. Passed? Passed as a parameter?

For example, below the main function, there is a inter type of interface reference,

It is common practice to create a class implementation interface before creating an object of the class. The question is, why does the function actually use an interface as an argument ?

But then we think that defining a class is a hassle and it can be simplified,

Why not emphasize more than three? Because the method is particularly long, it means that the anonymous inner class method body content is more, this object is too fat, reading is not.

Simplification is for reading, so it goes against the original intention.

This example is not an internal class, but the format of the anonymous inner class is wrong, and the class that implements the interface is actually an inner class.

Java-Preliminary Understanding-tenth-Anonymous inner class-application

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.