"Java Basics" (6) Inner class

Source: Internet
Author: User
Tags event listener

The inner class is not very well understood, but it is actually a class that contains another class.

As a person is composed of physical results, such as the brain, limbs, organs, or an internal class equivalent to one of the organs, such as the heart: it also has its own properties and behavior (blood, beating)

Obviously, it is not possible to use attributes or methods to represent a heart unilaterally, but a class

And the heart is in the body, just as the same inner class is inside the outside.

Example 1: The basic structure of an inner class
//External ClassclassOut {Private intAge = 12; //Inner class    classIn { Public voidprint () {System.out.println (age); }    }}  Public classDemo { Public Static voidMain (string[] args) {out.in in=NewOut ().NewIn ();        In.print (); //or access by sowing        /*out = new Out ();        Out.in in = Out.new in ();        In.print (); */    }}

Run Result:12

From the above example, it is not difficult to see that the inner class actually seriously destroys the good code structure, but why use the inner class?

Because inner classes are free to use member variables of external classes (including private ones) without generating objects of external classes, this is the only advantage of inner classes

Like the heart can directly access the body's blood, rather than through the doctor to draw the blood

After compiling the program, two. class files are generated, respectively Out.class and Out$in.class

Where $ represents the out.in in the above program.

Out.in in = new Out (). The new in () can be used to generate an object of the inner class, which has two small knowledge points to note

1. The first out is to indicate which outer class the inner class object needs to be generated

2. An object of an outer class must be preceded to generate an object of the inner class, because the inner class is intended to access member variables in the outer class

Example 2: member Inner class
classOut {Private intAge = 12; classIn {Private intAge = 13;  Public voidprint () {intAge = 14; System.out.println ("Local variable:" +Age ); System.out.println ("Inner class variable:" + This. Age); System.out.println ("Outer class Variable:" + out. This. Age); }    }}  Public classDemo { Public Static voidMain (string[] args) {out.in in=NewOut ().NewIn ();    In.print (); }}

Local variables: 14
Internal class variable: 13
External class variables: 12

As you can see from Example 1, an inner class that has no member variables and local variables of the same name, the inner class accesses the member variables of the outer class directly without specifying Out.this. Property name

Otherwise, local variables in the inner class will overwrite the member variables of the outer class

The member variable that accesses the inner class itself is available with the this. property name, and accessing the member variables of the external class requires the use of Out.this. Property name

Example 3: Static inner class
classOut {Private Static intAge = 12; Static classIn { Public voidprint () {System.out.println (age); }    }}  Public classDemo { Public Static voidMain (string[] args) {out.in in=Newout.in ();    In.print (); }}

Run Result:12

As you can see, if you statically internal static, the inner class can only access static member variables of the outer class, with limitations

Second, because the inner class is statically, the out.in can be viewed as a whole, and can be directly new to the object of the inner class (access to static through the class name, it doesn't matter if the external class object is generated)

Also called Static local class, nested inner class, is decorated as static inner classes. An inner class declared as static does not require a connection between an inner class object and an external class object, which means that we can refer directly to Outer.Inner, which means that you do not need to create an external class or create an inner class.

Example 4: Private Inner class
classOut {Private intAge = 12; Private classIn { Public voidprint () {System.out.println (age); }    }     Public voidOutprint () {NewIn (). print (); }}  Public classDemo { Public Static voidMain (string[] args) {//This method is not valid        /*out.in in = new Out (). New in ();        In.print (); */ out=Newout ();    Out.outprint (); }}

Run Result:12

If an inner class only wants to be manipulated by a method in an external class, you can use private to declare the inner class

In the above code, we have to create an in class object inside the out class, and we can no longer use out.in in = new Out (). New in () creates an object of the inner class

That is, the inner class at this point is controlled only by the outer class

As is, my heart can only be controlled by my body, others cannot access it directly

Example 5: Method Inner Class ( local inner class
classOut {Private intAge = 12;  Public voidPrint (Final intx) {classIn { Public voidInprint () {System.out.println (x);            System.out.println (age); }        }        NewIn (). Inprint (); }}  Public classDemo { Public Static voidMain (string[] args) {out-out=Newout (); Out. Print (3); }}

Operation Result:

3
12

In the above code, we move the inner class into the method of the outer class, and then regenerate it into an inner class object in the method of the outer class to invoke the inner class method

If we need to pass in a parameter to a method in the outer class at this point, then the method parameter of the outer classes must make

6 anonymous Inner class

Anonymous inner classes are the only classes that do not have constructors . Because it does not have a constructor, the use of anonymous internal classes is very limited, and most anonymous inner classes are used for interface callbacks.

Anonymous inner class should be the most used when we write code, it is convenient to use anonymous inner class when writing the code of Event listener, and make the code easier to maintain. The following code is an Android event listener code:

Scan_bt.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub                              }        }); History_bt.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub                              }        });

"Java Basics" (6) Inner class

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.