Java programming things 71-Introduction to internal classes

Source: Internet
Author: User

Java programming things 71mso-hansi-font-family: calibri "> -- Introduction to internal classes

Chen yuefeng

From: http://blog.csdn.net/mailbomb

 

8.10 inner class)

The internal class is a special syntax in the javamso-Hansi-font-family: calibri "> language. In short, it is to declare a class inside the class, these declarations are called internal classes in the class. In actual declaration, the internal class can be declared inside the class, the class method, or the class constructor, the syntax format of the internal class declaration is the same as that of the general class declaration, but the staticmso-Hansi-font-family: calibri "> modifier can be used for the internal class declaration.

This section does not provide an in-depth introduction to the use of internal classes. It just provides a brief description to help you establish the concept of internal classes and implement some basic usage.

The following is an example code of a simple internal class:

/**

* Basic sample code for internal classes

*/

Public class outclass {

Int I = 0;

Public class
Innerclass {

Public void
Test (){

I ++;

}

}

}

In this sample code, the class innerclasscalibri; MSO-Hansi-font-family: calibri "> is declared inside the class outclass, so innerclasscalibri; MSO-Hansi-font-family: calibri "> is called an internal class, while outclass is called an external class of innerclasscalibri; MSO-Hansi-font-family: calibri">.

After the code is compiled, two classmso-Hansi-font-family: calibri "> files are generated, and one is outclass. class, the other is outclass $ innerclass. classmso-ascii-font-family: calibri; MSO-Hansi-font-family: calibri ">. It should be noted that the internal class is also compiled into an independent class file, the class file name is: External class name $ MSO-Hansi-font-family: calibri "> internal class name. class.

In the internal class, you can easily access the attributes of the external class IMSO-Hansi-font-family: calibri ">, while the external class cannot directly reference the attributes and methods in the internal class, the Syntax provides a set of special formats to access the attributes and methods in the internal class. These syntaxes are not described here.

The internal class is a new syntax added after the birth of javamso-Hansi-font-family: calibri "> the syntax is designed with two original intentions:

1. Hide the implementation of internal classes

That is to say, only functions used by external classes are hidden inside the internal class. When other classes access this internal class, the syntax is more complex, thus, access to internal classes by other classes is avoided to some extent.

2. The internal class can access all attributes and methods of the external class to avoid passing parameters.

In the internal class, you can access all attributes and methods in the external class, privatecalibri; MSO-Hansi-font-family: calibri "> the attributes and Methods Modified by the Access Controller can also be accessed by internal classes, which facilitates the compilation of internal classes and avoids parameter passing, it also reduces the number of attributes and methods that external classes need to open to other classes.

Of course, in addition to a series of benefits, internal classes also bring about a series of shortcomings. The main shortcomings are as follows:

1. Added syntax complexity

Declaring a class inside a class increases the complexity of javamso-Hansi-font-family: calibri, the Java programming style is damaged to some extent, and the difficulty of reading the code is greatly increased.

2. Make the entire project structure complex

By increasing the complexity of each class, internal classes make the project structure more complex and not as clear as the original structure.

The syntax of internal classes is widely used in the following programming technologies:

1. Event Processing in graphic user interface programming

2. javamso-Hansi-font-family: calibri "> Implementation of some functional classes in language APIs

Based on the above description, if it is not necessary in the actual development process, we do not recommend writing internal classes. The functions implemented by internal classes can be implemented using calls between classes, in this way, you can retain the clear programming style of javamso-Hansi-font-family: calibri "> language.

Note: In the javamso-Hansi-font-family: calibri syntax, multiple classes can be written in the source code, for example:

/**

* Multiple classes are declared in a class.

*/

Public class twoclassinonefile {

Int n = 0;

Public void method (){}

}

 

Class anotherclass {

Char C;

Public void T (){}

}

Such source code will generate two independent classmso-Hansi-font-family: calibri "> files during compilation: twoclassinonefile. class and anotherclass. classcalibri; MSO-Hansi-font-family: calibri ">, the structure is not an internal class.

In actual projects, we do not recommend such encoding because it is inconvenient to search for the class source code. We recommend that you declare only one class in each source file.

 

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.