Nested classes and internal classes are used extensively in Java, and to avoid being difficult to understand, here are a few small examples of how to use them.
The structure of nested classes and inner classes such as
Static Nested Classes
A static nested class, which is a nested class declared outside of a class, can be called directly by the class name, without initialization, because it is static.
1 class out1{ 2 Private static String name = "Xingoo" ; 3 static class info{ 4 void print () { 5 System.out.pri Ntln ("Name:" +name); 6 7
8 }
Using the sample
PackageTestClass;classout1{Private StaticString name = "Xingoo"; Static classinfo{voidprint () {System.out.println ("Name:" +name); } }}classout2{Private StaticString name = "Xingoo Test 2"; Static classinfo{String name= "Inner infomation!"; voidPrintinner () {System.out.println ("Name:" +name); } voidPrintouter () {System.out.println ("Name:" +out2.name); } }}/*** Static Nesting class *@authorXingoo **/ Public classTest1 { Public Static voidMain (string[] args) {//When you create an object, you create it xxx.yyy this formatOut1.info Oi =NewOut1.info (); Oi.print (); Out2.info Oi2=NewOut2.info (); Oi2.printinner (); Oi2.printouter (); }}
View Codemember Inner class
A member inner class, that is, a member of another class, so you can create this class only if you reference another class.
1 class outer{2 Private String name = "Xingoo"; 3 class inner{4 void print () {5 System.out.println (name); 6 }7 }8 }
Usually also used to hide the implementation of an interface, as shown below, the first is the traditional implementation, the second is to hide the implementation of the interface to implement the way.
1 Interfaceprinter{2 voidprint (String name);3 }4 classOuterImpl1Implementsprinter{5 @Override6 Public voidprint (String name) {7 System.out.println (name);8 }9 }Ten classouterimpl2{ One Private classInnerImplementsprinter{ A @Override - Public voidprint (String name) { - System.out.println (name); the } - } - PublicPrinter GetPrinter () { - return Newinner (); + } -}
Using the sample
PackageTestClass;/*** Simple test member inner class *@authorXingoo **/classouter{PrivateString name = "Xingoo"; classinner{voidprint () {System.out.println (name); } }}Interfaceprinter{voidprint (String name);}classOuterImpl1Implementsprinter{@Override Public voidprint (String name) {System.out.println (name); }}classouterimpl2{Private classInnerImplementsprinter{@Override Public voidprint (String name) {System.out.println (name); } } PublicPrinter GetPrinter () {return Newinner (); }}/*** Member Inner class *@authorXingoo **/ Public classTest2 { Public Static voidMain (string[] args) {//creates an object of an outer class that creates an object of its inner class by invoking the new method of the objectOuter o =Newouter (); Outer.Inner I= O.Newinner (); I.print (); //inner class implements interface hidingPrinter OUT1 =NewouterImpl1 (); Out1.print ("Xingoo test1"); OUTERIMPL1 Out1trans= (OUTERIMPL1) out1;//support for down conversionPrinter Out2= (NewouterImpl2 ()). GetPrinter (); Out2.print ("Xingoo test2"); //cannot be converted downward because inner is of private type, which enables the interface to be hidden }}
View Codelocal inner class
The declaration of a local class functions within a block of code, so if a declared class is used only by a single program, it can be used in other places.
1 Interfacelogger{2 Public voidlog (String message);3 }4 Public classTest3 {5 6String StartTime = (NewDate ()). ToString ();7 /**8 * Local inner class, which, by definition, does not belong to any other class, because it is declared inside this class. Unlike anonymous inner classes, it has a name. 9 * @returnTen */ One PublicLogger GetLogger () { A //since Loggerimpl is only used internally within GetLogger, it is relatively safer to define it inside the function. - classLoggerimplImplementslogger{ - @Override the Public voidlog (String message) { -System.out.println (StartTime + ":" +message); - } - } + return NewLoggerimpl (); - } + A Public Static voidMain (string[] args) { atTest3 test =Newtest3 (); -Logger Logger =Test.getlogger (); -Logger.log ("Hello xingoo!"); - } -}
Anonymous Inner class
When you create an object using new, the implementation class is created directly behind it.
1 Abstract classprintable{2 Public voidprint (String name) {3 4 }5 }6 /**7 * Just after the object is created, define its class implementation directly.8 * @authorAdministrator9 */Ten Public classtest4 { One Public Static voidMain (string[] args) { APrintable printer =NewPrintable () { - Public voidprint (String name) { - System.out.println (name); the } -};//Note that because this is actually a new syntax, you need to add a semicolon; -Printer.print ("Hello!xingoo!"); - } +}
Reference: JAVA 7 Program Design
"Java Development Series"-nested classes and internal classes