PackageCom.lei.duixiang;/*** Local Inner class * 1, if you need to use a local variable in a method, the local variable needs to be set to final type * 2, in other words, the inner class defined in the method can only access local variables of the final type in the method * This is because the local variable defined in the method is equivalent to a constant, its raw Life cycle Beyond Method run * Cannot change the value of a local variable in an inner class because the local variable is set to final@authorAdministrator * * Anonymous inner class OUTCLASS4*/InterfaceOutinterface {}//define an interface Public classOUTCLASS3 {//local inner class PublicOutinterface doit (FinalString x) { //define an inner class in the doit () method classInnerClass2ImplementsOutinterface { PublicInnerClass2 (String s) {s=x; System.out.println (s); } } return NewInnerClass2 ("doit"); } /*** in the doit () method, first returns a Outinterface reference, and then inserts a custom inner class code in the return statement * because this class does not have a name, this is where the inner class becomes an anonymous inner class * essentially this The function of an inner class is to create an object that implements an anonymous class on the Outinterface interface. * All implementations of the anonymous class are required to be written between braces, with the following syntax: * return new A () {...//inner class}; * After the anonymous inner class definition is complete, you need to add a mark * This semicolon is not an identity that represents the end of the definition inner class, but rather represents the identity that creates the Outinterface reference expression *@authorAdministrator **/ Public classOUTCLASS4 {//Anonymous Inner class PublicOutinterface doit () {//defining the Doit () method return NewOutinterface () {//declaring an anonymous inner class Private inti = 0; Public intGetValue () {returni; } }; } } Public Static voidMain (string[] args) {OUTCLASS3 out=NewOUTCLASS3 (); Out.doit ("Outinterface doit"); }}
19. Local inner class and anonymous inner class