Java Internal classes

Source: Internet
Author: User

Excerpt from the https://www.zhihu.com/question/21373020

A very important design criterion is: The design is to do subtraction, can not use the pattern to use the pattern. This criterion applies equally to internal classes.

So it is not necessary to use the inner class without the inner class.

Previously felt that the internal class used to target the specific interface of the external class, rather than all of a sudden to give the whole object to others better.

For example, the following code in the External class outer implementation of three interface methods, can run, can fly, can think.

Then there are three methods Getrunner (), Getflyer (), Getthinker () with a targeted external exposure to some functional interfaces.


但实际上直接实现三个接口不是就很好嘛。用内部类根本算不上优雅,多了很多代码。
仅仅为了暴露接口根本不需要使用内部类。

Or, more commonly, a common control framework. We define an event interface and must have an action () method. Define the process of event handling in an external class.

It then defines several inner classes that implement the event interface.

But this is not a non-intrinsic class. Defining A, Class B, and finally combining the event object into the controller is perfectly possible and more concise and readable.

Also, when it comes to internal classes, closures and callbacks are often mentioned. But internal classes are not the only solution.

Simple inheritance, the combination can achieve the same data encapsulation effect.

There are still a lot of situations where there is no internal class that can't be handled, or it's more elegant to handle in-house classes.

Is still the first example. If an external class has more than one interface implementation method. If I implement the Runnable interface, I can only define a run () method. This is where the inner class comes in handy.

For example, penguins can run and swim. So it has two internal classes that implement two different run (). You can return two different runnable references.

Another typical scenario is multiple inheritance. If the outer class has inherited a base class, say penguins inherit from birds.

But Runnable is just an abstract class? Java does not support multiple inheritance, and internal classes can solve this problem.

Just mentioned that the control framework can be used without internal classes. But actually, you see, the event-driven swing is full of internal classes.

Why is it? Because there are a lot of events, and the corresponding methods of most events are only used once.

Internal classes are used to control the number of classes, and the consideration is to better encapsulate them. Inner class Another good feature is that it is independent of the external class,

will not be initialized together with the initialization of the outer class as the composition.

It's about creating it when we need it. For example, the iterator in the container needs to be created manually. exists as an optional component in an external class,

Does not increase the burden on external classes. In short, to know when you really need an internal class, first to figure out when you can not use the inner class.

Inner classes are not dianabol pills, do not misuse internal classes.

 

Java Internal classes

Related Article

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.