Instructions for using members ' internal classes
- The inner class can directly access the variables in the outer class and is not controlled by the access modifiers;
- After defining the member inner class, you must use an external class object to create the inner class object, that is, the inner class object name = External class. New inner Class ();
- After compiling the above program, you will find that there are two. class files//Where the second is the. class file for the outer class, and the first is the. class file for the inner class, that is, the. class file for the inner class of the member is always the case: external class name $ internal class name.
- an external class cannot use the members and methods of an inner class directly, you can first create an object of the inner class, and then access its member variables and methods through the objects of the inner class.
- If an external class and an 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 variables of the external class , you can use the This keyword .
External class HelloWorld
public class helloworld{
Private String name= "IMOOC";
int age=20;
public class inner{
String name= "Huangyl";
public void Show () {
System.out.println ("Name in the outer class:" +helloworld.this.name);
System.out.println ("Age in The Outer class:" +helloworld.this.age);
System.out.println ("Name in the Inner class:" +name);
}
}
Test member Inner Class
public static void Main (Sting () args) {
HelloWorld out=new HelloWorld (); //Create an object of an external class
Inner Iner = Out.new Inner (); //Create an object of the inner class
Iner.show ();
}
}
Member Inner classes in Java