The use of internal classes in Java development _java

Source: Internet
Author: User
In Java development, Java developers suggested that, as little as possible, the inner class should be used to bring the inner class to the class he was in and generate a separate class.
directly to the code:
Copy Code code as follows:

Package com.b510;

/**
* Calls between internal classes
*
* @author Hongten
*
* @time 2012-3-19 2012
*/
public class Innerclasstest {

public static void Main (string[] args) {
This is an example of innerclasstest.
Innerclasstest innerclasstest = new Innerclasstest ();
Innerclasstest.sayhelloworld ();

This is an instance of Class A
Innerclasstest.a ia = innerclasstest.new A ();
Ia.asayhello ();

This is an example of INNERCLASSB.
INNERCLASSTEST.A.INNERCLASSB IAB = Ia.new innerclassb ();
Iab.bsayhello ();

This is an example of INNERCLASSC.
INNERCLASSTEST.A.INNERCLASSB.INNERCLASSC iabc = Iab.new innerclassc ();
Iabc.csayhello ();

This is an example of INNERCLASSD.
INNERCLASSTEST.A.INNERCLASSD iad = ia.new innerclassd ();
Iad.dsayhello ();

This is an example of Innerclasse.
Innerclasstest.a.innerclassb.innerclassc.innerclasse IABCE = Iabc.new Innerclasse ();
Iabce.esayhello ();

Of course, when we call Innerclasse, we can write this.
Innerclasstest.a.innerclassb.innerclassc.innerclasse e = new Innerclasstest (). New A (). New Innerclassb (). New INNERCLASSC (). New Innerclasse ();
System.out.println ("====================== separator = = = =");
E.esayhello ();
}

Methods in the class Innerclasstest
public void SayHelloWorld () {
System.out.println ("I am test,helloworld!");
}

/**
* Define Class A, he is the inner class of the Innerclasstest class
*/
Class A {
Methods in Class A
public void Asayhello () {
System.out.println ("I am a,helloworld!");
}

/**
* Define INNERCLASSB class, he inherits Class A
*/
Class Innerclassb extends A {
Methods in the class INNERCLASSB
public void Bsayhello () {
System.out.println ("I am B,helloworld");
}

/**
* Define INNERCLASSC class, he is the inner class of INNERCLASSB
*/
Class INNERCLASSC {
Methods in the class INNERCLASSC
public void Csayhello () {
System.out.println ("I am c,helloworld!");
}

/**
* Define Innerclasse class, he is the inner class of INNERCLASSC
*/
Class Innerclasse {
Methods in the class Innerclasse
public void Esayhello () {
System.out.println ("I am e,helloworld!");
}
}
}
}

/**
* Define INNERCLASSD class, he is the internal class of a
*/
Class INNERCLASSD {
Methods in the class INNERCLASSD
public void Dsayhello () {
System.out.println ("I am d,helloworld!");
}
}
}
}

Operation Effect:
Copy Code code as follows:

I am test,helloworld!
 i am a,helloworld!
 i am B,helloworld
 i am c,helloworld!
 i am d,helloworld!
 i am e,helloworld!
 ====================== Separator = =
 i am e,helloworld!

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.