Dark Horse programmer _ Java internal class entry

Source: Internet
Author: User
Java internal class entry-jin wo Zang Jiao

Reference: http://blog.csdn.net/wuliming_ SC /article/details/1753164

------- Android training, Java training, and hope to communicate with you! ----------

Role of an internal class

Why design internal classes? One of my guesses is to control access permissions and avoid unnecessary access problems. Just like in real life, people will always have privacy, and there will always be things that don't want others to know but are especially important to themselves. An internal class can ensure that a class is only accessed by the class it contains, because the internal class has private access control, and the common class does not (or is not possible ).

I learned from the Internet that another role of internal classes is to improve a multi-Inheritance Mechanism. The following is the most attractive reason for using internal classes: Each internal class can independently inherit from one (Interface) implementation, so no matter whether the peripheral class has inherited a (Interface) implementation has no impact on internal classes. Without the ability of internal classes to inherit multiple specific or abstract classes, it is difficult to solve some design and programming problems. From this perspective, the internal class makes the multi-inheritance solution complete. The interface solves some problems, while the internal class effectively implements "Multi-inheritance ".

Member internal class Introduction

The internal class of the member can be divided into static and non-static methods. Because it is an entry point, only non-static methods are introduced.

First, external class methods access internal class variables

Step 1: create an internal class inner = new inner ();

Step 2: call internal class variables through objects.

Note: It is the same as accessing non-External classes.

Second, internal class methods access external class variables and their own Variables

If variables with the same name as external variables

External access: External class name. This. variable name (exclusive access method of internal class) Outer. This. j

Access itself: This. variable name this. I

Else

You can directly use the variable name to access both yourself and the outside.

Conclusion: the difficulty lies in the fact that the external and internal variables have the same name. When an external class accesses an internal class, it treats the internal class as a common class. When an internal class accesses an external class, it regards itself as a member of the external class.

Introduction to three anonymous internal classes

To put it simply, an anonymous internal class is an internal class without a name.

If the following conditions are met, it is appropriate to use an anonymous internal class.

When using anonymous internal classes, remember the following principles:

★Only one instance of the class is used.

★Class is used immediately after definition.

★The class is very small (Sun recommends following four lines of code)

★Naming a class does not make your code easier to understand.

★No constructor is allowed for anonymous internal classes.

★Anonymous internal classes cannot define any static members, methods, and classes.

★Anonymous internal classes cannot be public, protected, private, or static.

★Only one instance of the anonymous internal class can be created.

★An anonymous internal class must be behind New, and uses it to implicitly implement an interface or implement a class.

★Because the anonymous internal class is a local internal class, all restrictions on the local internal class take effect.

In swing graphical interface programming, it is useful for anonymous internal classes.

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.