1. Internal class
Inner class --- the class contained in the class
External class - a class that contains a class other than an internal class is an external class
1. Member inner class - equivalent to member variables in java classes
Basic structure:
package com.object.test1;
public class TestClass {
public class InClass{
}
}
TestClass -- external class
InClass -- member inner class
After the internal class is compiled, a new bytecode file will be formed [external class name $internal class name. Class]
1. Any access restriction modifier can be used for member inner classes
package com.object.test1; public class TestClass { public class InClass{ //Variables defined by the public access restriction modifier public String name="zhangsan"; //The default access restriction modifier defines the variable int age=23; //Variables defined by protected access restriction modifiers protected String address="Xi'an"; //Private access restriction modifier defined variable private int id=1001; } }
2. Member internal classes can have instance variables, instance methods and construction methods, and cannot have static elements.
package com.object.test1; public class TestClass { public class InClass{ //Member internal classes can have instance variables, instance methods, and construction methods, and cannot have static elements. //Instance variable public int id=1001; //Member inner classes cannot have static variables //public static String name="zhangsan"; // report errors //Construction method public InClass(){ System.out.println("InClass Member inner class constructor"); } //Example method public void testMethod(){ System.out.println("InClass Member inner class instance method"); } //Static method //Member inner classes cannot have static methods //public static staticMethod() {} / / an error is reported } }
3. Constructors in member internal classes can access other constructors [new],
Can access instance variables / METHODS: [object. Instance variables / methods] , This. Instance variable / method, object / this can be omitted]
package com.object.test1; public class TestClass { public class InClass{ //Member internal classes can have instance variables, instance methods, and construction methods, and cannot have static elements. //Instance variable public int id=1001; //Member inner classes cannot have static variables //public static String name="zhangsan"; // report errors //Construction method //The inner class of InClass member has no parameter constructor public InClass(){ System.out.println("InClass Member inner class parameterless constructor"); } //The inner class of an InClass member has a parameter constructor public InClass(String name){ //Constructors in inner classes of members can access other constructors [new] //You can access instance variables / methods [object. Instance variables / methods] , This. Instance variable / method, object / this can be omitted] new InClass(); //Inner class object new InClass().id=1002; this.id=1003; id=1004; new InClass().testMethod(); this.testMethod(); testMethod(); System.out.println("InClass Member inner class has parameter constructor"); } //Example method //Internal class testMethod instance method of InClass member public void testMethod(){ } //Internal class testMethod2 instance method of InClass member public void testMethod2(){ } }
4. Instance methods in member internal classes can access the constructor [new]
Instance variable / method can be accessed: [object. Instance variable / method, this. Instance variable / method, object / this can be omitted]
package com.object.test1; public class TestClass { public class InClass{ //Member internal classes can have instance variables, instance methods, and construction methods, and cannot have static elements. //Instance variable public int id=1001; //Member inner classes cannot have static variables //public static String name="zhangsan"; // report errors //Construction method //The inner class of InClass member has no parameter constructor public InClass(){ System.out.println("InClass Member inner class parameterless constructor"); } //The inner class of an InClass member has a parameter constructor public InClass(String name){ //Constructors in inner classes of members can access other constructors [new] //You can access instance variables / methods [object. Instance variables / methods] , This. Instance variable / method, object / this can be omitted] new InClass(); //Inner class object new InClass().id=1002; this.id=1003; id=1004; new InClass().testMethod(); this.testMethod(); testMethod(); System.out.println("InClass Member inner class has parameter constructor"); } //Example method //Internal class testMethod instance method of InClass member public void testMethod(){ //The instance method in the inner class of a member can access the constructor [new] //Instance variable / method can be accessed [object. Instance variable / method, this. Instance variable / method, object / this can be omitted] new InClass(); //Inner class object new InClass().id=1002; this.id=1003; id=1004; new InClass().testMethod2(); this.testMethod2(); testMethod2(); System.out.println("InClass Member inner class testMethod Example method"); } //Internal class testMethod2 instance method of InClass member public void testMethod2(){ System.out.println("InClass Member inner class testMethod2 Example method"); } }
5. The constructor / instance method in the internal class of the member can access the constructor, instance method / variable and class method / variable of the external class.
package com.object.test1; public class TestClass { //Instance variable public int age=23; //Static member variable public static String name="lisi"; //Construction method public TestClass(){ System.out.println("TestClass Nonparametric construction method"); } //Example method public void shiliMethod(){ System.out.println("TestClass Parameterless instance method"); } //Static method public static void staticMethod(){ System.out.println("TestClass Parameterless instance method"); } public class InClass{ //Member internal classes can have instance variables, instance methods, and construction methods, and cannot have static elements. //Instance variable public int id=1001; //Member inner classes cannot have static variables //public static String name="zhangsan"; // report errors //Construction method //The inner class of InClass member has no parameter constructor public InClass(){ System.out.println("InClass Member inner class parameterless constructor"); } //The inner class of an InClass member has a parameter constructor public InClass(String name){ //Constructors in inner classes of members can access other constructors [new] new InClass(); //Inner class object new InClass().id=1002; this.id=1003; id=1004; new InClass().testMethod(); this.testMethod(); testMethod(); //Accessing external variables and methods //Constructor in inner class of member can access instance variable / method [object. Instance variable / method] , This. Instance variable / method, object / this can be omitted] new TestClass(); //Accessing external class instance variables new TestClass().age=25; TestClass.this.age=27; age=29; //Accessing external class static variables new TestClass().name="wangwu"; TestClass.this.name="zhaowu"; name="zhubajie"; //Access external class instance methods new TestClass().shiliMethod(); TestClass.this.shiliMethod(); shiliMethod(); //Accessing static methods of external classes new TestClass().staticMethod(); TestClass.this.staticMethod(); staticMethod(); System.out.println("InClass Member inner class has parameter constructor"); } //Example method //Internal class testMethod instance method of InClass member public void testMethod(){ //The instance method in the inner class of a member can access the constructor [new] //Instance variable / method can be accessed [object. Instance variable / method, this. Instance variable / method, object / this can be omitted] new InClass(); //Inner class object new InClass().id=1002; this.id=1003; id=1004; new InClass().testMethod2(); this.testMethod2(); testMethod2(); //Accessing external variables and methods //Instance methods in member internal classes can access instance variables / methods [object. Instance variables / methods] , This. Instance variable / method, object / this can be omitted] new TestClass(); //Accessing external class instance variables new TestClass().age=25; TestClass.this.age=27; age=29; //Accessing external class static variables new TestClass().name="wangwu"; TestClass.this.name="zhaowu"; name="zhubajie"; //Access external class instance methods new TestClass().shiliMethod(); TestClass.this.shiliMethod(); shiliMethod(); //Accessing static methods of external classes new TestClass().staticMethod(); TestClass.this.staticMethod(); staticMethod(); System.out.println("InClass Member inner class testMethod Example method"); } //Internal class testMethod2 instance method of InClass member public void testMethod2(){ System.out.println("InClass Member inner class testMethod2 Example method"); } //Static method //Member inner classes cannot have static methods //public static staticMethod() {} / / an error is reported } }
6. The constructor / instance method in the external class can access the constructor, instance method / variable of the member's internal class, and the constructor / instance method in the external class
Class methods cannot access member inner classes. [only objects can be accessed, objects cannot be omitted, and this access is not allowed]
package com.object.test1; public class TestClass { //Instance variable public int age=23; //Static member variable public static String name="lisi"; //Construction method public TestClass(){ //Accessing variables and methods in internal classes new InClass(); //Inner class object new InClass().id=1002; //this.id=1003; //id=1004; new InClass().testMethod(); //this.testMethod(); //testMethod(); System.out.println("TestClass Nonparametric construction method"); } //Example method public void shiliMethod(){ new InClass(); //Inner class object new InClass().id=1002; //this.id=1003; //id=1004; new InClass().testMethod(); //this.testMethod(); //testMethod(); System.out.println("TestClass Parameterless instance method"); } //Static method public static void staticMethod(){ //new InClass(); // Inner class object //new InClass().id=1002; //this.id=1003; //id=1004; //new InClass().testMethod(); //this.testMethod(); //testMethod(); System.out.println("TestClass Parameterless instance method"); } public class InClass{ //Member internal classes can have instance variables, instance methods, and construction methods, and cannot have static elements. //Instance variable public int id=1001; //Member inner classes cannot have static variables //public static String name="zhangsan"; // report errors //Construction method //The inner class of InClass member has no parameter constructor public InClass(){ System.out.println("InClass Member inner class parameterless constructor"); } //The inner class of an InClass member has a parameter constructor public InClass(String name){ } //Example method //Internal class testMethod instance method of InClass member public void testMethod(){ ; System.out.println("InClass Member inner class testMethod Example method"); } //Internal class testMethod2 instance method of InClass member public void testMethod2(){ System.out.println("InClass Member inner class testMethod2 Example method"); } } }
7. Other classes can access members' internal classes, and need to rely on external class objects. Pay attention to the access restriction modifier.
package com.object.test1; public class OtherClass { //Construction method public OtherClass(){ TestClass tcl=new TestClass(); TestClass.InClass inc1=tcl.new InClass(); TestClass.InClass inc2=new TestClass().new InClass(); inc1.id=1004; inc2.id=1005; inc1.testMethod(); inc2.testMethod(); } //Example method public void shiliOtherClass(){ TestClass tcl=new TestClass(); TestClass.InClass inc1=tcl.new InClass(); TestClass.InClass inc2=new TestClass().new InClass(); inc1.id=1004; inc2.id=1005; inc1.testMethod(); inc2.testMethod(); } //Static method public static void staticOtherClass(){ TestClass tcl=new TestClass(); TestClass.InClass inc1=tcl.new InClass(); TestClass.InClass inc2=new TestClass().new InClass(); inc1.id=1004; inc2.id=1005; inc1.testMethod(); inc2.testMethod(); } }
2. Method internal class - the class defined in the method, which is equivalent to a local variable
1. Basic format:
package com.object.test2; public class TestClass { //Method inner class in constructor public TestClass(){ class InTestClass1{ } } //Method inner class in instance method public void testMethod(){ class InTestClass2{ } } //Method inner classes in static methods public static void staticMethod(){ class InTestClass3{ } } }
1. Method inner classes cannot use any access restriction modifiers.
2. The internal class of the method can have instance variables / methods and construction methods, and cannot have static elements.
3. The constructor / instance method of the internal class of the method can access the constructor in the internal class of the method, and the instance method / variable [object / this, can also be omitted].
4. The constructor / instance method in the method's internal class can access the local variable of this method. By default, final will be used to modify the local variable [direct variable name].
5. The internal class of the method can access the construction method, instance method / variable and class method / variable of the external class.
6. External classes cannot access method internal classes.
package com.object.test2; public class TestClass { //Method internal classes can access the construction methods, instance methods / variables, and class methods / variables of external classes. //Instance variable public int num=23; //Static member variable public String nam="wangwu"; //Construction method public TestClass(){} //Example method public void testshiliClass(){} //Static method public static void staticMethod(){} //Method inner class in instance method public void testMethod(){ class InTestClass2{ //Method internal classes can have instance variables public int id=1001; //Method inner class cannot have static elements //public static String name="zhangsan"; //Method internal classes can have instance methods, construction methods, public InTestClass2(){} public InTestClass2(int age){ //The constructor of the inner class of this method can be accessed from the constructor of the inner class of this method new InTestClass2(); //The instance method / variable [object / this, can also be omitted] in the internal class of this method can be accessed in the constructor of the internal class of this method new InTestClass2().shiliMethod1(); this.shiliMethod1(); shiliMethod1(); new InTestClass2().id=1002; //Method internal classes can access the construction methods, instance methods / variables, and class methods / variables of external classes. new TestClass(); new TestClass().nam="lilu"; TestClass.this.nam="wangwu"; nam="liuqian"; new TestClass().num=27; TestClass.this.num=32; num=30; new TestClass().staticMethod(); TestClass.this.staticMethod(); staticMethod(); new TestClass(). testshiliClass(); TestClass.this.testshiliClass(); testshiliClass(); } public void shiliMethod1(){} public void shiliMethod2(){ //The constructor of the inner class of this method can be accessed from the constructor of the inner class of this method new InTestClass2(); //The instance method / variable [object / this, can also be omitted] in the internal class of this method can be accessed in the constructor of the internal class of this method new InTestClass2().shiliMethod1(); this.shiliMethod1(); shiliMethod1(); new InTestClass2().id=1002; new TestClass(); new TestClass().nam="lilulu"; new TestClass().num=29; new TestClass().staticMethod(); new TestClass(). testshiliClass(); //Method inner classes cannot have static methods //public static void Instatic(){} } } } }
3. Static nested classes -- equivalent to static variables in java classes
Basic format:
package com.object.test3; public class TestClass { //Static Nested Class public static class InClass{ } }
1. Static nested classes can have construction methods, instance variables / methods, and class variables / methods.
2. The constructor / instance method in the static nested class can access the constructor, instance variable / method and class variable / method in the static nested class.
3. The class method in the static inner class can access the constructor in this static nested class, instance variable / method [object only], class variable / method
4. Constructor / instance method / class method in static internal class can access constructor of external class, instance variable / method can only be object, class variable / method.
5. this. Is not allowed in static nested classes
6. Construction methods / instance methods / class methods of external classes can be accessed. Construction methods, instance variables / methods [object only], class variables / methods in static internal classes
7. Static nested classes can be accessed in other classes [new external class name. Static nested class name ()]. Note the access restriction modifier
package com.object.test3; public class TestClass { //Instance variable public String nam="zhangsan"; //Static member variable public static int num=112; //Construction method public TestClass(){ new InClass(); new InClass().id=1002; //this.id=1003; //id=1004; new InClass().age=25; // this.age=26; //age=28; new InClass().shiliMethod1(); //this.shiliMethod1(); //shiliMethod1(); new InClass().staticMethod(); InClass.staticMethod(); // this.staticMethod(); //staticMethod(); } //Example method public void testClassMethod(){ new InClass(); new InClass().id=1002; //this.id=1003; //id=1004; new InClass().age=25; // this.age=26; //age=28; new InClass().shiliMethod1(); //this.shiliMethod1(); //shiliMethod1(); new InClass().staticMethod(); InClass.staticMethod(); // this.staticMethod(); //staticMethod(); } //Static method public static void testClassStatic(){ new InClass(); new InClass().id=1002; //this.id=1003; //id=1004; new InClass().age=25; // this.age=26; //age=28; new InClass().shiliMethod1(); //this.shiliMethod1(); //shiliMethod1(); new InClass().staticMethod(); InClass.staticMethod(); // this.staticMethod(); //staticMethod(); } //Static Nested Class public static class InClass{ //Static nested classes can have construction methods, instance variables / methods, and class variables / methods. public int age=23; public static int id=1001; public InClass(){} public InClass(String name){ //Constructors in static nested classes can access constructors, instance variables / methods, and class variables / methods in this static nested class. new InClass(); new InClass().id=1002; this.id=1003; id=1004; new InClass().age=25; this.age=26; age=28; new InClass().shiliMethod1(); this.shiliMethod1(); shiliMethod1(); new InClass().staticMethod(); this.staticMethod(); staticMethod(); new TestClass(); new TestClass().nam="wahaha"; //this.nam="zhaoxi";// report errors //nam="zhaoliu"; //TestClas.this.nam="zhaoxi";// report errors new TestClass().num=234; TestClass.num=244; //this.num=245;// report errors //TestClass.this.num=247;// report errors num=211; new TestClass().testClassMethod(); //TestClass.this.testClassMethod(); //testClassMethod(); new TestClass().testClassStatic(); TestClass.testClassStatic(); //TestClass.this.testClassStatic(); //this.testClassStatic(); testClassStatic(); } //Example method public void shiliMethod1(){} public void shiliMethod2(){ //The instance method in the static nested class can access the constructor, instance variable / method, and class variable / method in the static nested class. new InClass(); new InClass().id=1002; this.id=1003; id=1004; new InClass().age=25; this.age=26; age=28; new InClass().shiliMethod1(); this.shiliMethod1(); shiliMethod1(); new InClass().staticMethod(); this.staticMethod(); staticMethod(); new TestClass(); new TestClass().nam="wahaha"; //this.nam="zhaoxi";// report errors //nam="zhaoliu"; //TestClas.this.nam="zhaoxi";// report errors new TestClass().num=234; TestClass.num=244; //this.num=245;// report errors //TestClass.this.num=247;// report errors num=211; new TestClass().testClassMethod(); //TestClass.this.testClassMethod(); //testClassMethod(); new TestClass().testClassStatic(); TestClass.testClassStatic(); //TestClass.this.testClassStatic(); //this.testClassStatic(); testClassStatic(); } //Static method public static void staticMethod(){} public static void staticMethod2(){ //The class method in the static inner class can access the constructor, instance variable / method [object only], class variable / method in this static nested class new InClass(); new InClass().id=1002; //this.id=1003; id=1004; new InClass().age=25; //this.age=26; //age=28; new InClass().shiliMethod1(); //this.shiliMethod1(); //shiliMethod1(); new InClass().staticMethod(); //this.staticMethod(); staticMethod(); new TestClass(); new TestClass().nam="wahaha"; //this.nam="zhaoxi";// report errors //nam="zhaoliu"; //TestClas.this.nam="zhaoxi";// report errors new TestClass().num=234; TestClass.num=244; //this.num=245;// report errors //TestClass.this.num=247;// report errors num=211; new TestClass().testClassMethod(); //TestClass.this.testClassMethod(); //testClassMethod(); new TestClass().testClassStatic(); TestClass.testClassStatic(); //TestClass.this.testClassStatic(); //this.testClassStatic(); testClassStatic(); } } }
package com.object.test3; public class OtherClass { public void otherMethod(){ //Static nested classes can be accessed in other classes [new external class name. Static nested class name ()]. Note the access restriction modifier TestClass. InClass into =new TestClass. InClass(); new TestClass. InClass().age=12; into.id=1005; into.shiliMethod1(); into.staticMethod(); } }
4. Anonymous inner class
1. For a java class without a name, build a subclass of the current class without creating a new class
There is no name, so it is called anonymous inner class
2. Basic format:
package com.object.test4; public class Person { public void personIn(){ System.out.println("Person Class"); } }
package com.object.test4; public class TestMain { public static void main(String[] args) { Person per=new Person(){ public void personIn(){ System.out.println("rewrite Person Class"); } }; per.personIn(); } }
new Person(){ public void personIn(){ System.out.println("rewrite Person Class"); } };
The above code is in the form of an anonymous inner class, which is equivalent to a subclass of the Person class, but it has no name, so it is called an anonymous inner class.
3. Inherited anonymous inner class
For example:
package com.wangxing.test5; public abstract class Person { public abstract void perinfo(); }
package com.wangxing.test5; public class TestClass { public void testClassShili(Person person){ person.perinfo(); } }
package com.object.test5; public class TestMain { public static void main(String[] args) { TestClass tcl=new TestClass(); tcl.testClassMethod(new Person(){ @Override public void personIn() { System.out.println("Overridden abstract class method"); } }); } }
4. Interface type anonymous inner class
For example:
package com.object.test6; public interface Person { void interfaceperson(); }
package com.object.test6; public class TestClass { public void testClassMethod(Person person){ person.interfaceperson(); } }
package com.object.test6; public class TestMain { public static void main(String[] args) { //When the parameter of a method is an interface type, you can pass the interface callback object / subclass of the current interface //Whether it is a subclass of the interface callback object / current interface, you must create a subclass //Anonymous inner classes can complete parameter transfer without creating subclasses TestClass tcl=new TestClass(); tcl.testClassMethod(new Person(){ @Override public void interfaceperson() { System.out.println("Overridden abstract class method"); } }); } }
Advantage: avoid creating additional independent subclasses.
Disadvantages: not easy to understand, not easy to read.