/**
* Inner class??
* Defined in an internal class, called an inner class.
* Inner class has a class body, method body
* The class in which the inner class is located is called an external class.
* ---------------------------------
The meaning of *① inner class
* To a certain extent, the effective protection of the code.
*② syntax
* Class outer{////External class
* Member variables
* Member method
* Class inner{///Inner classes
* Member variables
* Member method
* }
* }
Creation of *③ class objects
*//Create an external class object
* Outer Outer =new Outer ();
*//Create inner class object
* Inner inner=new Inner ();//This creates the wrong
*//Cannot create an inner class object directly, but you can create an external class object by first
* Then create an inner class object from an external class object.
For example
* Outer outer=new Outer ();
* Outer.Inner inner2=new Outer (). New Inner ();
* OR
* Outer.Inner inner2=outer.new Inner ();
*
* The inner class references the member variables of the outer class
* Two cases
*a. The member variables of the external class do not exist in the inner class.
* The name of the member variable of the outer class can be used directly in the inner class.
*class Outer {
*string name;
*class inner{
*public void test{
*name= "CC";//is the name of the outer class;
*}
*}
*}
*b. The member variables of the outer class exist in the inner class, and the inner class wants to use the member variables of the outer class.
* Must have an external class name. This member variable name
*class outer{
*string name;
*class inner{
*string name;
*public void Test () {
*name= "CC";//is the name of the inner class;
*outer.this.name= "DD";//the name of the external class at this time
*}
*}
*}
*
* @param args
*/
Package test05;
public class Innerclass {
public static void Main (string[] args) {
TODO auto-generated method stubs
Outer outer=new Outer ();
Outer.name= "Zhang San";
outer.age=18;
Outer.introduce ();
System.out.println ("-------------------------------");
Outer.Inner inner=outer.new Inner ();
Inner.name= "John Doe";
inner.age=20;
Inner.introduce ();
}
}
Class outer{/////External class
String name;
int age;
void introduce () {
System.out.println ("name" +this.name+ "Age" +this.age);
}
Class inner{/////Inner Classes
String name;
int age;
void introduce () {
System.out.println ("Inner class name:" +this.name+ "inner class Age: \ n" +this.age+ "external class name:" +outer.this.name+ "outside class Age:" +outer.this.age ");
}
}
}
Inner classes in Java