Java internal class Summary

Source: Internet
Author: User

// 2004-9-18
Summary of internal classes:

1. the nesting of internal classes can be understood as the link between classes. When constructing an internal class (non-static) instance, the instance of the outer class will be directly
As a parameter is passed into the constructor, And the instance of the outer class is also constructed using its instance of the outer class, so it is a chain-like relationship, the internal class can use all
The instance of the outer class, and the outer class cannot use the inner class, apply. this (outermost instance),. b. c. this (the instance of an outer layer in the chain ).

Class theouter {
Class KK {
Class Gg {
Int TT = 100;
}
}

Class AA {
Class BB {
Int JJ = 10;
Class CC {
{Int FF = theouter. AA. BB. This. JJ;
// Unable to directly access Gg without Link
System. Out. println (ff );
}
}
}
}
}

2. Creating an internal class new AA (). New BB (). New () CC actually calls the outer class instance. New Local Constructor ()
If the internal class is static, the instance AA. BB. CC test = new AA. BB. CC () of Multiple Static nested internal classes is not required for the instance of the outer class ();

3
Class A0 {
Class B0 {
}
}

Class vvv {
Class FF extends a0.b0 {// FF this is not an internal class.
Ff (A0 KK ){
KK. Super ();
}
}
}
Inherits from the Super () of the outer class of the parent class to be called by the internal class ()

4
Class dd {
Void print (){
System. Out. println ("DD. println is there ");
}
Class KK {
Kk (int I ){}
Class CC {
Void pp (){
System. Out. println ("****");
}
Void ddpp (){
Dd. This. Print ();
}
}
}
}

Class AA {
Public static void main (string AGRs []) {
Dd. KK. CC test = new dd (). New kk (10). New CC ();
Test. ddpp ();
}
}
The outer class can be used inside the class, but the upper class access function is not provided if the handle of an instance is generated.

5 This refers to the instance of this class.

6 default new
Class CC {
{
New Test ();
}
Class KK {
{
System. Out. println ("There KK ");
}
}

Class test {
{
New kk ();
}
Class KK {
{
System. Out. println ("There test_kk ");
}
}
}
}
New can be used directly, and the range of new can be the outer layer. This. New and this. New, but if the inner and outer layers have the same name, the internal class takes priority.

7
// When there is a link, note that the class name should not be repeated.
Class VV {
Class BB {}//!
Class KK {
// Class VV {}
Class BB {}//! This prevents errors.
}
}

8
Class A0 {
Class B0 {
}
}

Class vvv {
Class FF extends a0.b0 {// not an internal class.
Ff (A0 KK ){
KK. Super ();
}
}
}
Inherits from the Super () of the outer class of the parent class to be called by the internal class ()

9 anonymous internal class
Interface oo {
// Oo (INT g ){}
Int K = 10;
Void Pt ();
}

Class pp {
PP (INT g ){
}
Int get (){
Return 100;
}
}
 
Class me {
/*
Interface oo {
Void Pt ();
}
*/
Class KK {
OO testmed (){
Final int I = 10;
Int G = 100;
Return New OO (){
Int KK;
{
// Use the name of an external object as final.
// Internal inheritance interface priority
// The anonymous internal class has no constructor, and only blocks can be used for initialization.
Kk = I + K;
}
Public void Pt (){
System. Out. println (kk );
}
};
}
PP testmed0 (){
Return New PP (10) {// inherit from Class
Public int get (){
Return super. Get () * 10;
}
};
}
}
}
The anonymous internal class can inherit from the interface or the class, and the constructor that inherits the class can have parameters.

10
Class BF {
Interface KK {}
Class AA {
// The interface can be nested in the interface (unrestricted), or nested in the top layer, it won't work below
// Interface Gg {}
}
}

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.