Package com.t_06;import Org.junit.test;import com.t_06.StaticClass.StaticInnerClass;/** * The definition of a class is placed inside another class, and this class is called an inner class * @author Administrator **/ Public classFirst { Public classcontents{//like this, contents is called an inner class, the inner class knows the outer class, and can communicate with it . Public voidf () {System. out. println ("In class first ' s inner class Contents method F ()"); }} @Test Public voidFun () {Second Second=NewSecond (); Second.setstr ("Bird"); Second.contents Contents=second.NewContents ();//creating an inner class object uses a perimeter class object. How the new inner class object iscontents.f (); } /** * At this time, the inner class is private, it can be outside the class painter, no one can access * This private inner class gives the designer of the Class A way to completely block any dependent type of encoding and completely hide the implementation details
*/@Test Public voidfun2 () {Painter Painter=NewPainter (); Shape Shape=Painter.getshape (); Shape.paint (); } /** * Method Inner class * You can create a class within a method * It is worth noting that the class created inside the method cannot be accessed with the modifier * Other classes inside the method are not created when the method is called, they are compiled*/ Public voidtest1 () {classinner{ Public voidmethod () {System. out. println ("classes created inside a method"); } }} @Test Public voidFun3 () {Painter Painter=NewPainter (); Shape Shape=Painter.getshape1 (); Shape.paint (); } /** * in Implemention1 and 2 anonymous internal classes are used where the field is initialized, so does the defined factory method look more comfortable? * No feeling! */@Test Public voidFun4 () {service (implemention1.factory); Service (implemention2.factory); Servicefactory Factory1=implemention1.factory; Service Service1=Factory1.getservice (); Service1.method1 (); Servicefactory Factory2=implemention2.factory; Service Service2=Factory2.getservice (); Service2.method1 (); } Public voidService (Servicefactory Factory) {Service service=Factory.getservice (); Service.method1 (); } @Test Public voidfun5 () {staticclass.staticinnerclass inner=NewStaticclass.staticinnerclass (); System. out. println (Inner.getnum ()); }}classsecond{/** * When an inner class object is created, he has a connection to the peripheral object that created it, so it can access all members of the perimeter class without any special conditions. */ PrivateString str; PublicString getstr () {returnstr; } Public voidsetstr (String str) { This. str =str; } Public classcontents{ Public voidf () {System. out. println ("first.str="+str); } }}/** internal class and upward transformation * @author Administrator **/Interfaceshape{ Public voidpaint ();}classpainter{Private classInnershape implements shape{ Public voidpaint () {System. out. println ("Painter Paint () method"); } } PublicShape Getshape () {return NewInnershape (); } /** * Anonymous inner class, note that there will be a semicolon, if an anonymous inner class needs to use a parameter, then the parameter must be final type * Anonymous inner class does not have a constructor, if you want to imitate the constructor effect, you can take the code block {} **/ PublicShape GetShape1 () {return NewShape () { Public voidpaint () {System. out. println ("Painter Paint () method"); } {System. out. println ("Init method!"); } }; }}/** * Modified factory method by anonymous internal class **/Interfaceservice{ Public voidmethod1 ();}Interfaceservicefactory{Service getService ();}classImplemention1 implements service{ Public voidmethod1 () {System. out. println ("In Implemention1 method Method1 ()"); } Public StaticServicefactory factory=Newservicefactory () { PublicService GetService () {return NewImplemention1 (); } }; }classImplemention2 implements service{ Public voidmethod1 () {System. out. println ("In Implemention2 method Method1 ()"); } Public StaticServicefactory factory=Newservicefactory () { PublicService GetService () {return NewImplemention2 (); } }; }/** Nested class * Static inner class is called nested class * Nested class is an exception, when using nested classes NOTE: * Nested classes are created directly from new * in a nested class, you cannot access the non-static members of an external class like a normal inner class * can have s in nested classes Tatic method, this is not allowed in different inner classes. */classstaticclass{Private intnum; Private Static intsum=2; Public Static classstaticinnerclass{ Public intGetnum () {returnsum; } }}/** * Why internal class * A, inner class provides some kind of window into the outer class. * B, each inner class can inherit a class independently, regardless of whether the parent class has inherited a class*/
Java Internal Class review