Define internal classes in external classes

Source: Internet
Author: User

 

Class outer {
Private Static int size; // static variable

Public class inner {
Private int size;

Public void dostuff (INT size ){
Size ++; // access local variables
This. Size ++; // access the member variables of its internal class
Outer. This. Size ++; // access the member variables of its external class (which can be static variables)
System. Out. println (size + "" + this. Size + "" + outer. This. size );
}
} // Internal class inner ends

Public void testinner () {/** instance method defined in the outer class testinner () method */
Inner I = new inner ();
I. dostuff (5 );}

Public static void main (string [] ){
Outer o = new outer ();
O. testinner ();
} // End of class Outer

  

Example 2:

Import java. AWT .*;
Import java. AWT. event .*;
Public class innerclass {
Private frame F;
Private textfield TF;
Public innerclass (){
F = new frame ("inner classes example ");
TF = new textfield (30 );
}
Public voidi launchframe (){
Label Label = new label ("Click and drag the mouse ");
F. Add (Label, borderlayout. North );
F. Add (TF, borderlayout. South );
F. addmousemotionlistener (New mymousemotionlistener ();/* the parameter is an internal class object */
F. setsize (300,200 );
F. setvisible (true );
}
Class mymousemotionlistener extends mousemotionadapter {/* internal class start */
Public void mousedragged (mouseevent e ){
String S = "Mouse dragging: x =" + E. getx () + "Y =" + E. Gety ();
TF. settext (s );}
}
Public static void main (string ARGs []) {
Innerclass OBJ = new innerclass ();
OBJ. launchframe ();
}
}
The following code defines two internal classes and their call relationships for external classes:

Class Outer
{
Int outer_x = 100;
Private class innerone
{// Private internal class
Public int inner_y = 10;
Private int inner_z = 9;
Int inner_m = 5;
Public void display (){
System. Out. println ("display outer_x:" + outer_x );
}

}
Public innerone getinnerone (){
Return new innerone ();
}
Class innertwo {
Innerone innerx = getinnerone (); // accessible
Public void show (){
// System. Out. println (inner_y); // The y member of innterone cannot be accessed.
// System. Out. println (inner. inner_y); // you cannot directly access any member or method of innerone.
}
}
Void test (){
// System. Out. println ("inner Y:" + inner_y); // internal variables cannot be accessed.
}
}
Public class test {
Public static void main (string ARGs []) {
Outer outer = new outer ();
// Outer. innerone A = outer. getinnerone ();
// The innerone class is private, and the external class cannot be accessed. If the innerone class is public, it can.
Outer. Test ();
}
}

The innerone and inntertwo internal classes are only known within the scope of the class Outer. The variable members of the internal class are only visible within the internal class. If the external class or internal class at the same level needs to be accessed, you must use the method in the sample program to directly access internal class variables.

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.