1. Understanding anonymous Inner class/* Anonymous inner class is a simplified way of writing inner classes. Premise: There is a class or interface where the class can be either a concrete class or an abstract class. Anonymous inner class format: New class name or interface name () {overriding method;} This represents a subclass of what is the nature of the object? is an anonymous object that inherits the class or implements the interface's subclass. */interface Inter {public abstract void Show ();p ublic abstract void Show2 ();} Class Outer {public void method () {//The interface has only one method in it, only show ()/*new Inter () {public void Show () {////The method of overriding an interface or abstract class inside an anonymous inner class. System.out.println ("Show");}}. Show (); *///two methods when/*new Inter () {public void Show () {System.out.println ("show");} public void Show2 () {System.out.println ("Show2");}}. Show (); new Inter () {public void Show () {System.out.println ("show");} public void Show2 () {System.out.println ("Show2");}}. Show2 (); *///if there are many ways, it will be very troublesome. So choose to use parameters to receive this object, and then use the object to invoke the method on it. Inter i = new Inter () {//Interface implementation subclass object to interface, polymorphic public void Show () {System.out.println ("show");} public void Show2 () {System.out.println ("Show2");}};/ /note here the semicolon i.show (); I.show2 ();}} Class InnerClassDemo6 {public static void main (string[] args) {Outer o = new Outer (); O.method ();}} 2, anonymous internal class in the development of the use of a case for a simple demonstration: interface person { &nbsP;public abstract Void Study ();} Class Persondemo { public void Method (person p) {//This way, as described earlier, when the parameter type is a reference type. See Blogs (http://blog.csdn.net/qq_32059827/article/details/51344046) p.study (); }}//Implementation Class class Student implements person { public Void study () { & nbsp; system.out.println ("Good study, Day up"); }}class InnerClassTest2 { public static void Main (string[] args) { //test persondemo PD = new Persondemo (); person p = new Student (); pd.method (P); system.out.println ("--------------------"); //use of anonymous internal classes in development //The results are exactly the same as above, using anonymous inner classes to save memory space. Using scenarios, the method is used less often. pd.method (New person () { public void Study () { System.out.println ("Good study, Day up"); } }); }}
Object-Oriented (anonymous inner class)
for the first season of getting Started with Java