20210220内部类
https://mubu.com/doc/75Nn9c3thOq
package mianshi_0219; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; public class InnerClassTest { public static void main(String[] args){ TalkingClock clock =new TalkingClock(); clock.start(1000,true); JOptionPane.showMessageDialog(null, "Quit program?"); } } class TalkingClock{ // private int interval; // private boolean beep; // // public TalkingClock(int interval,boolean beep){ // this.interval=interval; // this.beep=beep; // } public void start(int interval,boolean beep){ // class TimePrinter implements ActionListener{ // public void actionPerformed(ActionEvent event){ // System.out.println("At the tone,the time is"+new Date()); // if(beep) Toolkit.getDefaultToolkit().beep(); // } // } ActionListener listener=new ActionListener(){ public void actionPerformed(ActionEvent event){ System.out.println("At the tone,the time is"+new Date()); if(beep) Toolkit.getDefaultToolkit().beep(); } }; Timer t=new Timer(interval,listener); t.start(); } }
package mianshi_0219; public class StaticInnerClassTest { public static void main(String[] args) { double[] d = new double[20]; for (int i = 0; i <d.length ; i++) { d[i] = 100 *Math.random(); ArrayAlg.Pair p = ArrayAlg.minmax(d); System.out.println("min = "+p.getFirst()); System.out.println("max = "+p.getSecond()); } } static class ArrayAlg{ public static class Pair{ private double first; private double second; public Pair(double f,double s){ first = f; second = s; } public double getFirst(){ return first; } public double getSecond(){ return second; } } public static Pair minmax(double[] values){ double min = Double.POSITIVE_INFINITY; double max = Double.NEGATIVE_INFINITY; for (double v:values) { if (min > v)min = v; if (max < v)max = v; } return new Pair(min,max); } } }