Functions of internal classes

Source: Internet
Author: User

I recently learned the hadoop source code and found that floatwritable and so on all have internal classes. I reviewed the functions of internal classes. floatwritable should be used for the purposes of 3 and 4.

 

The following content is reproduced: http://blog.csai.cn/user1/42856/archives/2008/29228.html

I. Definition
The class placed inside a class is called an internal class.
Ii. Functions
1. Internal classes can be well hidden
Generally, non-Internal classes do not allow private and protected permissions, but internal classes can
2. The internal class has the permission to access all elements of the peripheral class.
3. But implement multi-Inheritance
4. You can avoid modifying interfaces to call two methods with the same name in the same class.
Iii. Example
1. Hide
In general, the access permissions to classes are restricted by the access modifier before the class. Generally, non-Internal classes do not allow private and protected permissions, however, internal classes are acceptable, so we can hide our information through internal classes. See the following example.
Interface
Package insidecategory;

Public interface incrementable
{
Void increment ();
}
Classes
Package insidecategory;

Public class example {

Private class insideclass implements interfacetest
{
Public void test ()
{
System. Out. println ("this is a test ");
}
}
Public interfacetest getin ()
{
Return new insideclass ();
}
}
The bold section above is an internal class and the access modifier is private.
Client Program
Package insidecategory;

Public class testexample {

Public static void main (string ARGs [])
{
Example A = new example ();
Interfacetest a1 = A. getin ();
A1.test ();
}
}
The bold part is the code called by the client. From this code, I only know
The getin () method returns an interfacetest instance, but I do not know that this instance is implemented in this way. Because insideclass is private, we cannot see the name of the specific class if we don't look at the code, so it can be well implemented and hidden.
2. unconditional access to all elements of the peripheral class
Package insidecategory;

Public class tagbean {

Private string name = "liutao ";
Private class intest
{
Public intest ()
{
System. Out. println (name );
}
}
Public void test ()
{
New intest ();
}
Public static void main (string ARGs [])
{
Tagbean BB = new tagbean ();
BB. Test ();
}
}
In the bold section above, the name variable is a private variable defined in the tagbean. This variable can unconditionally access system. Out. println (name );
3. Multi-Inheritance
Is very important, I personally think it is one of the biggest reasons for the existence of internal classes. It is precisely because of its existence that the Inheritance Mechanism of Java is improved. We all know that Java can only inherit one class. Its Multi-inheritance is implemented using interfaces before we learn internal classes. But sometimes there are a lot of inconveniences when using interfaces. For example, we must implement all the methods in an interface. The internal classes are different. It can make our class inherit from multiple concrete classes or abstract classes. Let's take a look at the example below.
Category 1
Package insidecategory;

Public class example1 {

Public string name ()
{
Return "liutao ";
}
}
Category 2
Package insidecategory;

Public class example2 {

Public int age ()
{
Return 25;
}
}

Category 3
Package insidecategory;

Public class mainexample
{
Private class test1 extends example1
{
Public string name ()
{
Return super. Name ();
}
}
Private class Test2 extends example2
{
Public int age ()
{
Return super. Age ();
}
}
Public string name ()
{
Return new test1 (). Name ();
}
Public int age ()
{
Return new Test2 (). Age ();
}
Public static void main (string ARGs [])
{
Mainexample MI = new mainexample ();
System. Out. println ("name:" + MI. Name ());
System. Out. println ("Age:" + MI. Age ());
}
}
Note that Class 3 implements two internal classes test1, Test2, test1, and example1 respectively. Test2 inherits example2, in this way, class 3 mainexample has the methods and attributes of example1 and example2, thus indirectly implementing multi-inheritance.
4. Avoid modifying interfaces to call two methods with the same name in the same class.
Imagine that if your class needs to inherit a class and implement an interface, but you find that the inherited class and interface have two methods with the same name, what should you do? How do you differentiate them ?? This requires our internal class. See the following code.
Interface
Package insidecategory;

Public interface incrementable
{
Void increment ();
}

Class myincrement
Package insidecategory;

Public class myincrement {

 Public void increment ()
{
System. Out. println ("other increment ()");
}
Static void F (myincrement F)
{
F. increment ();
}

}
The two methods are the same. The following class inherits the two classes.
If you do not need an internal class
Package insidecategory;

Public class callee2 extends myincrement implements incrementable
{
Public void increment ()
{
// Code
}
}
I would like to ask if the increment () method is a method that overwrites myincrement here? Or incrementable. How can I adjust to myincrement? Obviously, this is difficult to differentiate. If we use an internal class, we can solve this problem well. See the following code:
Package insidecategory;

Public class callee2 extends myincrement
{
Private int I = 0;
Private void incr ()
{
I ++;
System. Out. println (I );
}
Private class closure implements incrementable
{
Public void increment ()
{
Incr ();
}
}
Incrementable getcallbackreference ()
{
Return new closure ();
}
}
We can use internal classes to implement interfaces so that they do not conflict with the methods of peripheral 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.