Go to: Java internal class

Source: Internet
Author: User
Tags multiple inheritance in c
In Java, there is a kind of class called inner class, also known as nested class, which is defined inside other classes. An internal class is a member of its external class. Like other members, it can directly access the data and methods of its external class. However, compared with external classes, only public and default modifiers are different. An internal class can be any modifier as a member. During compilation, the internal class name is outerclass $ innerclass. Class.


1. Internal class Access Data Variables
In some cases, when the variables defined in the internal class are the same as those in the external class, how can we ensure correct access to every variable?

1.1 calling internal class methods directly from external classes in main

Class Outer
{
Private int Index = 10;
Class inner
{
Private int Index = 20;
Void print ()
{
Int Index = 30;
System. Out. println (this); // The object created from the inner
System. Out. println (outer. This); // The object created from the outer
System. Out. println (INDEX); // output is 30
System. Out. println (this. Index); // output is 20
System. Out. println (outer. This. Index); // output is 10
}
}

Void print ()
{
Inner inner = new inner (); // get internal class reference
Inner. Print ();
}
}

Class Test
{
Public static void main (string [] ARGs)
{
Outer outer = new outer ();
Outer. Print ();
}
}
Here, the internal class inner keyword this points to the internal class inner object. to point to the external class object, you must add the external class name before the this pointer, indicates that this is the debris that points to the external class structure, such as outer. this.

1.2 explicitly returning internal class references in main

Class Outer
{
Private int Index = 10;
Class inner
{
Private int Index = 20;
Void print ()
{
Int Index = 30;
System. Out. println (INDEX );
System. Out. println (this. Index );
System. Out. println (outer. This. Index );
}
}

Inner getinner ()
{
Return new inner (); // returns a reference to an internal class.
}
}

Class Test
{
Public static void main (string [] ARGs)
{
Outer outer = new outer ();
Outer. Inner inner = outer. getinner ();
Inner. Print ();
}
}
Inner is the internal class of outer. Therefore, the internal class must be identified by attribute references in class test.

1.3 when the main method is inside the outer class

Class Outer
{
Private int Index = 10;
Class inner
{
Private int Index = 20;
Void print ()
{
Int Index = 30;
System. Out. println (INDEX );
System. Out. println (this. Index );
System. Out. println (outer. This. Index );
}
}

Inner getinner ()
{
Return new inner (); // returns a reference to an internal class.
}

Public static void main (string [] ARGs)
{
Outer outer = new outer ();
Inner inner = outer. getinner (); // note the changes here.
Inner. Print ();
}
}
Because the main method is inside the outer, it can be referenced directly without attribute references.


1.4 directly generate internal class objects in the main method

Class Test
{
Public static void main (string [] ARGs)
{
Outer outer = new outer ();
Outer. Inner inner = outer. New inner (); // note the changes here.
Inner. Print ();
}
}
When using the new constructor to construct an external class object, an internal class object is not constructed together. Therefore, when you need to access the internal class method, you must use the new operator to construct an internal class object for this external class object.

2. Local internal class
The internal class defined in the method is a local internal class. It can only access local variables of the final type in the method, because the local variables defined by final are equivalent to a constant, the life cycle is extended, so that the internal class of the method can still access the variable when it dies. In addition, it can also reference variables and methods that define external classes. In addition, some internal classes in the method body cannot have access modifiers.

Class Outer
{
Int num = 10;
Public void print (final int initialize GS)
{
Class inner
{
Int num = 20;
Public inner ()
{
System. Out. println ("this is inner."); // you can see that it is different from anonymous internal class usage.
}

Public void print ()
{
Int num = 30;
System. Out. println (this); // The object created from the local inner
System. Out. println (Num );
System. Out. println (this. Num );
System. Out. println (outer. This. Num );
System. Out. println (Bytes GS );
}
}
Inner inner = new inner (); // This sentence must be placed behind the inner of the definition class.
Inner. Print ();
}

Public static void main (string [] ARGs)
{
Outer outer = new outer ();
Outer. Print (40 );
}
}
For local class naming, whether it is to define multiple classes in a method or define classes separately in several methods, the name after compilation is: outerclass $ 1innerclass. Class

3. Anonymous internal class
As a special internal class, anonymous internal classes not only have the characteristics of common internal classes, but also have their own unique features:
An anonymous internal class must expand a base class or implement an interface, but there cannot be explicit extends and implements clauses;
Anonymous internal classes must implement the parent class and all abstract methods in the interface;
Anonymous internal classes always use the non-argument constructor of the parent class to create instances. If an interface is implemented, the constructor is object ();
After the anonymous internal class is compiled, it is named outerclass $ n. class, where n is an integer starting from 1. If Multiple anonymous internal classes are defined in a class, the number starts from 1 in the order they appear.

Abstract Class
{
Abstract Public void sayhello ();
}

Class Outer
{
Public static void main (string [] ARGs)
{
New outer (). callinner (new ()
{
Public void sayhello ()
{
System. Out. println (this); // The object created from the anonymous inner
System. Out. println ("Hello! ");
}
});
}

Public void callinner ()
{
A. sayhello ();
}
}

4. static internal class
Compared with non-static internal classes, static internal classes do not reference external classes. In addition, no non-static internal class can have static data, static methods, or another static internal class (internal class nesting can be more than one layer ). However, static internal classes can have all of this. This is the second difference between the two. A static internal class can declare a static member. The static internal class can access the static methods and members (including Private Static members) of the peripheral class ). When static internal classes are instantiated, you can directly instantiate internal classes without first instantiating the peripheral classes. For non-static internal classes, the external classes must be instantiated before they can be instantiated.

5. Inheritance of internal classes
When a class inherits from an internal class, the default constructor is unavailable. The following syntax must be used:
Class withinner
{
Class inner
{
Public void sayhello ()
{
System. Out. println ("hello .");
}
}
}

Public class test extends withinner. Inner
{
Test (withinner WI)
{
WI. Super ();
}
Public static void main (string [] ARGs)
{
Withinner Wi = new withinner ();
Test test = new test (WI );
Test. sayhello ();
}
}
Because each internal class has a reference pointing to an external class, when inheriting an internal class, you must first create an external class and call the constructor of its internal class through this external class reference. If the inherited internal class is a static internal class, you don't need to do this. Just call Super;

6. Two special usage of internal classes
A class derives from another class and implements an interface. But the methods defined in the interface have different meanings from those defined in the parent class, you can use the internal class to solve this problem.
Interface Machine
{
Void run ();
}

Class person
{
Void run ()
{
System. Out. println ("Run ");
}
}

Class robot extends person
{
Private class machineheart implements Machine
{
Public void run ()
{
System. Out. println ("heart run ");
}
}

Machine getmachine ()
{
Return new machineheart ();
}
}

Class Test
{
Public static void main (string [] ARGs)
{
Robot robot = new robot ();
Machine M = robot. getmachine ();
M. Run ();
Robot. Run ();
}
}
In the robot class, use the internal machineheart class to implement the run method of the interface machine. At the same time, the robot class inherits the run method of the parent class person. If the robot directly implements the interface machine without using the internal machineheart class, how can I call the run method of the parent class?

Internal classes can be used to solve the problem of multiple inheritance in C ++.
Class
{
Void fn1 ()
{
System. Out. println ("It's fn1 .");
}
}

Abstract class B
{
Abstract void FN2 ();
}

Class C extends
{
B getb ()
{
Return new B ()
{
Public void FN2 ()
{
System. Out. println ("It's fn2 .");
}
};
}
}

Class Test
{
Public static void main (string [] ARGs)
{
C = new C ();
C. fn1 ();
C. getb (). FN2 ();
}
}
Class C should inherit both Class A and Class B, then it can put the definition of Class B inside class C to make it an internal class.


In general, when we need to implement an interface in a certain situation, and in another case, we do not need to implement this interface, we can use internal classes to solve this problem. Let Internal classes implement this interface. Another good reason is that Java internal classes and interfaces can effectively implement multiple inheritance.
References: http://hi.baidu.com/kiddoneal/blog/item/3b6a68864fd75a20c75cc348.html
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.