1. Inner class:
1 /*2 local Inner class: Defines another class within a method of a class, and another class is called a local inner class. 3 4 local inner classes to note the details:5 1. If the local inner class accesses a local variable, the local variable must use the final decoration,6 7 8 */9 classouter{Ten OneString name= "Name of the outer class"; A - Public voidTest () { - //Local Variables the Final inty = 100;//when does y disappear from memory? Y disappears after the method finishes executing. - - //local inner class - classinner{/* + when the test method finishes executing, then y disappears from memory immediately, and the inner object is in the method - the execution has not disappeared from memory, and the print method of the inner object is still accessing + Y variable , when the y variable is gone, it gives the impression that Y's life variable has been extended. A . at - Solution: If a local inner class accesses a local variable, then let the local inner class - access the replica of this local variable. - */ - intx = 10; - in Public voidprint () { -System.out.println ("This is the print method for the local inner class:" +y); to } + } - theInner Inner =NewInner ();//when does this inner object disappear? The life cycle of the inner object is longer than the life cycle of the local variable Y. * inner.print (); $ }Panax Notoginseng - the } + A the classDemo5 + { - Public Static voidMain (string[] args) $ { $Outer Outer =NewOuter (); - outer.test (); - } the}
Note: If there is no statice, it is necessary to instantiate the inner class and make it an object. If there is a statice, then only the instance of the partial class can be. The inner class and the outer class are of a lateral approach.
2. Anonymous inner class:
1. Anonymous internal classes under inheritance
1 /*2 3 anonymous inner class: A class without a class name is called an anonymous inner class. 4 5 the benefits of anonymous inner classes: simplify writing. 6 7 the use of anonymous inner classes is premised on the existence of an inheritance or implementation relationship before it can be used. 8 9 Ten Anonymous inner classes are generally used for arguments. One A */ - - Abstract classanimal{ the - Public AbstractAnimal Run (); - - Public Abstract voidsleep (); + } - + classouter{ A at Public voidprint () { - //Requirement: Define a class within a method to inherit the animal class, and then call the Run method with sleep (). - - /* - //local inner class - class Dog extends animal{ in - Public void Run () { to System.out.println ("The dog is running.."); + } - the Public void Sleep () { * System.out.println ("Dog lying in Open eyes sleep."); $ }Panax Notoginseng } - the //Create Objects + Dog d = new Dog (); A D.run (); the d.sleep (); + */ - //Anonymous inner class: Anonymous inner class there is just no class name, and all the other members are. $ //an anonymous inner class is an inherited relationship with animal. The object that is currently creating the animal subclass. $Animal A =NewAnimal () {// polymorphic - - //members of anonymous internal the PublicAnimal Run () { -System.out.println ("The dog is running.."));Wuyi return This; the } - Wu Public voidsleep () { -System.out.println ("The dog is lying awake in the open eyes.")); About } $ - //a unique approach - Public voidBite () { -System.out.println ("The dog is biting people.")); A } + the }; - $ a.bite (); the A.run (); the a.sleep (); the the } - } in the classDemo6 the { About Public Static voidMain (string[] args) the { the //System.out.println ("Hello world!"); the +Outer Outer =NewOuter (); - outer.print (); the Bayi } the}
2. Anonymous inner class under interface implementation
1 //Implementing an anonymous inner class under a relationship2 Interfacedao{3 4 Public voidAdd ();5 }6 classouter{7 8 Public voidprint () {9 //create an object of an anonymous inner classTen NewDao () {//not an interface cannot create an object? What's the matter with you now? One A Public voidAdd () { -System.out.println ("Add Success"); - } the }.add (); - } - } - + classDemo7 - { + Public Static voidMain (string[] args) A { atTestNewDao () { - - Public voidAdd () { -SYSTEM.OUT.PRINTLN ("Add Employee Success"); - } - }); in - to } + //Call this method ... - Public Static voidTest (Dao D) {//the formal parameter type is an interface reference : the D.add (); * } $}
Java inner class and anonymous inner class