The Inner class-----is the class in the other class
1. Definition of inner class
- Define class as Class xxxx{...} Inside a class, you can '
- Build Xxxx$xxxx class file at compile time for internal classes
- Inner class cannot have the same name as an external class
2, the use of internal classes
- Use this class in a class that encapsulates its class province, using the same method as the normal class
- Use this class elsewhere: the class name of an outer class is preceded by a class name; the outer class name when you create an inner class object with new. Inner class name inner class object name = Outer class object name. New inner class name (parameter)
3. Using members of an external class in an inner class
- The inner class can use all the fields and methods of the outer class directly; private can also
- If the inner class has a field method with the same name as the outer class, the external class name. this. field or method
code example:
Public classTestinnerclass { Public Static voidMain (string[] args) {externalclass extern=NewExternalclass (); Externalclass.innerclass Inner= extern.NewInnerclass (); Inner.output (The field of the method); }}classexternalclass{PrivateString s = "outer class field"; classinnerclass{PrivateString s = "Inner class field"; Public voidOutPut (String s) {System.out.println (The value of "s" is "+s); System.out.println (The value of "THIS.S" + This. s); System.out.println (The value of "EXTERNALCLASS.S" is "+ Externalclass". This. s); } } }
View Code
Output Result:
The value of S is the field of the method This The value of . S is the Inner class field externalclass.s the value of the External class field
View Code
Several special classes in Java---inner classes