What is an internal class
An inner class (Inner Class) is a class that is defined in another class. Corresponding to this, the class containing the inner class is called the outer class.
Inner class action
1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class
2. The method of the inner class can access all the data of the external class directly, including the private data
3. Functions implemented by internal classes can also be implemented using external classes, but sometimes it is easier to use internal classes
There are several internal classes
- member Inner class
- Method Inner Class
- Static Inner class
- Anonymous inner class
1. member Inner class
A member inner class exists that is dependent on an external class, that is, if you want to create an object of the member's inner class, as long as an object of an outer class must exist.
The member inner class is the most common inner class, defined as being inside another class, as in the following form:
//External class HelloWorld Public classHelloWorld {//Inner class inner, class inner inside class HelloWorld Public classInner {//methods for inner classes Public voidShow () {System.out.println ("I Love cjj!"); } } Public Static voidMain (string[] args) {//creating an external class objectHelloWorld Hello =NewHelloWorld (); //creating an inner class objectInner i = hello.NewInner (); //methods for invoking inner class objectsi.show (); }}
Here HelloWorld belongs to an external class, while the Inner class belongs to an inner class.
When invoking an inner class, the first is to instantiate the outer class, for example:HelloWorld hello = new HelloWorld ();
The inner class is then instantiated, for example:Inner i = hello. New Inner ();
The members of the inner class are then invoked through the objects implemented by the inner class, for example:i.show ();
Attention:
When a member inner class has a member variable or method with the same name as an external class, a hidden phenomenon occurs, that is, by default, members of the members inner class are accessed.
If you want to access a member of the same name as an external class, you need to access it in the following form:
The external class. This . Member variable external class. this. Member method
Although members of the inner class can access the members of an external class unconditionally, the external class would like to access members of the member's inner class but not so much.
In an external class, if you want to access members of a member's inner class, you must first create an object of the member's inner class, and then access it by referring to the object
Instance:
//External Class Public classOuter {Private intA = 99;//private properties of external classes//Inner class inner Public classinner{intb = 2;//member properties for inner classes Public voidTest () {System.out.println ("Access A in an external class:" +a); System.out.println ("Access B in inner class:" +b); } } //Test member Inner class Public Static voidMain (string[] args) {Outer o=NewOuter ();//create an external class object with the object name aInner i = O.NewInner ();//Create an inner class object with an external class object named II.test ();//call the test method of an inner class object }}
Operation Result:
accessing a:99 in an external class accesses the B: 2 in the inner class
As we can see from the code above, the use of the members ' inner classes is as follows:
1. The Inner class is defined inside the Outer class and is equivalent to the position of a member variable of the Outer class, and the Inner class can use any access control, such as public, protected, private, etc.
2. The test () method defined in the Inner class can directly access the data in the Outer class without being affected by the access control, such as direct access to private property A in the Outer class
3, after defining the member inner class, you must use the Outer class object to create the inner class object, but not directly to the new inner class object, namely: the inner class object name = Outer class object. New inner Class ();
4, after compiling the above program, you will find that two. class files have been generated
Where the second is the. class file for the outer class, the first is the. class file for the inner class, that is, the. class file for the member's inner class is always the case: the outer class name $ internal class name. class
In addition, friendly hints OH:
1. External classes cannot use the members and methods of the inner class directly.
You can first create an object of an inner class and then access its member variables and methods through the objects of the inner class.
2. If the external class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variable of the external class, you can use the This keyword.
such as:HelloWorld. This. Name
//External class HelloWorld Public classhelloworld{//Private property of the external class name PrivateString name = "Imooc"; //member properties for external classes intAge = 20; //member Inner class inner Public classInner {String name= "Adoration Class"; //methods in the inner class Public voidShow () {System.out.println ("Name in the External class:" + HelloWorld. This. Name); //Access member variables for external classes, you can use the This keyword System.out.println ("Name in the Inner class:" +name); System.out.println ("Age in the Outer class:" +Age ); } } //Test member Inner class Public Static voidMain (string[] args) {//create an object of an external classHelloWorld o =NewHelloWorld (); //to create an object of an inner classInner i = O.NewInner (); //call the Show method of an inner class objecti.show (); }}
2. Method Inner Class
The inner class of the method is the inner class defined in the method of the outer class, and the inner class of the method is visible only within the method, that is, it can be used only inside the method.
//External Class Public classMouter {//methods in the outer class Public voidShow () {Final intA = 25;//Constants intb = 13;//variables//method Inner Class classminner{intc = 2;//variables in the inner class Public voidprint () {System.out.println ("Accessing constants in external methods a:" +a); System.out.println ("Access variable C for inner class:" +c); }} minner mi=NewMinner ();//Create a method within an objectMi.print ();//methods for calling inner classes } //test method Inner class Public Static voidMain (string[] args) {mouter mo=NewMouter ();//creating an external class objectMo.show ();//methods for calling external classes }}
Note : The local inner class is like a local variable inside a method and cannot have public, protected, private, or static modifiers.
3. Method Inner Class
Static inner classes are internal classes of static adornments, which are characterized by:
1. Static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external Class (). Members
2. If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. Static member.
If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name
3. When creating an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner Class ();
//External Class Public classSOuter {Private intA = 99;//private variables for external classes Static intb = 1;//static variables for external classes//static inner class Public Static classsinner{intb = 2;//variables for inner classes Public voidTest () {System.out.println ("Access B in the external class:" +souter.b); System.out.println ("Access B in inner class:" +b); } } //testing static Internal classes Public Static voidMain (string[] args) {Sinner Si=NewSinner ();//directly create an object of the inner classSi.test ();//Call the test () method }}
4. Anonymous inner class
Not to be continued
Java Inner class