Inner class:
A non-static inner class
//non-static inner class//a non-static inner class can call local variables of an external class, whether or not private//to instantiate an inner class in an external class: Innerclass inner = new Innerclass ();//instantiate internal classes in other classes: 1. Test.innerclass inner = new test.new innerclass ();//2.Test t = new Test ();//T.innerclass inner = t.new innerclass ();//classtest{inti = 1; classinnerclass{ Public voidrun () {System.out.println ("outclass field =" +i); } } Public voidrun () {innerclass inner=NewInnerclass (); } }classruntest{Test T=NewTest (); T.innerclass Inner= T.NewInnerclass (); //orTest.innerclass inner =NewTest.Newinnerclass ();}
Two local inner classes
/*** Local Inner class * 1. Local inner classes and local variables function the same, only in local scope valid * 2. The member variable of the outer class can be called directly in the local inner class * 3. External classes Create local inner class objects Inclass Inclass = new Inclass (); * * *
*/ Public classOutclass {inti = 8; Public voidrun () {classinclass{ Public voidrun () {System.out.println ("Run Field i =" +i); }} inclass Inclass=NewInclass (); Inclass.run (); } }
Three static inner classes
/*** Static Inner class * 1. Static internal classes also have access to external class member variables * 2. External class instantiation Inner class Inclass inclass = new Inclass (); * * **/ Public classOutclass {Static intt = 6; Static classinclass{inti = 5; intj =T; } Public voidrun () {Inclass Inclass=NewInclass (); System.out.println (INCLASS.I); }}
Four anonymous inner classes
Standard Creation Method
New anonymous{};
Use
/*** Anonymous Inner class * **/ Public classINNERCLASS4 { Public Static voidMain (string[] args) {Thread T=NewThread () { Public voidrun () { for(inti = 1; I < 6; i++) {System.out.println (i+ "Y"); } } }; T.start (); Runnable R=NewRunnable () { Public voidrun () { for(inti = 6; I < 11; i++) {System.out.println (i+ "U"); } } }; Thread T1=NewThread (R); T1.start (); }}
Java--4 type of inner class