Java Learning local inner class, anonymous inner class, Static inner class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.