Write an anonymous inner class and static internal class program, the local inner class feeling is the latter two general situation, the latter two is the former special application.
Local inner class: Defines a class in a method that cannot be declared with public or static, can be completely hidden from the outside, and only that method can be called.
Anonymous inner class: Do not write the definition of the header part of the class, directly write the class body, I personally do not feel very good, only when the interface to define the function is better? This class cannot have constructors, so it calls the constructor of the superclass, noting that the inner class cannot have parameters when implementing the interface.
Static inner class: If it's just Tankun in order to hide a class without using an inner class to invoke the perimeter object, you can use this class, which is somewhat like a arrays.sort ().
Package Staticinnerclass;public class Staticinnerclasstest{public static void Main (string[] args) {double[] D=new double [20];for (int i=0;i<20;i++) d[i]=100*math.random (); Arrayalg.pair P=arrayalg.minmax (d); System.out.println (P.getfirst ()); System.out.println (P.getsecond ());}} Class Arrayalg{public static class Pair{private double First;private double second;public Pair (double first,double second ) {This.first=first;this.second=second;} Public double GetFirst () {return this.first;} Public double Getsecond () {return this.second;}} public static Pair Minmax (double[] value) {Double min=double.max_value;double max=double.min_value;for (double v:value) {if (min>v) min=v;if (max<v) max=v;} return new Pair (Min,max);}}
Package Anonymousinnerclass;import Java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*; Import Javax.swing.timer;public class Anonymousinnerclasstest{public static void Main (string[] args) {Talkingclock Clock=new Talkingclock (); Clock.start (1000,true); Joptionpane.showmessagedialog (NULL, "Quit the program?"); System.exit (0);}} Class talkingclock{public void start (int interval,boolean beep) {ActionListener listener=new ActionListener () {public void actionperformed (ActionEvent event) {Date now=new date (); System.out.println ("At the tone,the time is" +now), if (beep) toolkit.getdefaulttoolkit (). Beep ();}}; Timer t=new timer (interval,listener); T.start ();}}
Java Learning local inner class, anonymous inner class, Static inner class