类:
package innerClass; import timer.TimePrinter; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; public class TalkingClock { private int interval; private boolean beep; public TalkingClock(int interval, boolean beep) { this.interval = interval; this.beep = beep; } public void start() { ActionListener listener = new TimePrinter(); Timer timer = new Timer(interval, listener); timer.start(); } public class TimerPrinter implements ActionListener { @Override public void actionPerformed(ActionEvent actionEvent) { System.out.println("At the tone, the time is " + new Date()); //内部类既可以访问自身的数据域,也可以访问创建它的外围类对象的数据域 if (beep) Toolkit.getDefaultToolkit().beep(); } } }
测试:
package innerClass; import javax.swing.*; public class InnerClassTest { public static void main(String[] args) { TalkingClock talkingClock = new TalkingClock(1000, true); talkingClock.start(); JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); } }
if (TalkingClock.this.beep)
ActionListener listener = this.new TimerPrinter();
package innerClass; import timer.TimePrinter; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; public class TalkingClock { private int interval; private boolean beep; public TalkingClock(int interval, boolean beep) { this.interval = interval; this.beep = beep; } public void start() { //局部类不能用public或private访问说明符声明。它的作用域被限定在这个局部类的块中。 class TimerPrinter implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { System.out.println("At the tone, the time is " + new Date()); //内部类既可以访问自身的数据域,也可以访问创建它的外围类对象的数据域 if (beep) Toolkit.getDefaultToolkit().beep(); } } ActionListener listener = new TimerPrinter(); Timer t = new Timer(interval, listener); t.start(); } }
package innerClass; import timer.TimePrinter; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; public class TalkingClock { public void start(int interval, boolean beep) { //局部类不仅可以访问包含它们的外部类,还可以访问局部变量(局部变量必须为final) class TimerPrinter implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { System.out.println("At the tone, the time is " + new Date()); //内部类既可以访问自身的数据域,也可以访问创建它的外围类对象的数据域 if (beep) Toolkit.getDefaultToolkit().beep(); } } ActionListener listener = new TimerPrinter(); Timer t = new Timer(interval, listener); t.start(); } }
package anonymousInnerClass; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import javax.swing.Timer; public class TalkingClock { public void start(int interval, boolean beep) { //创建一个实现ActionListener接口的类的新对象,需要实现的方法actionPerforme定义在括号{}内 //匿名类不能有构造器,构造器参数传递给超类构造器 ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("At the tone, the time is " + new Date()); if (beep) Toolkit.getDefaultToolkit().beep(); } }; Timer timer = new Timer(interval, listener); timer.start(); } }
类:
package staticInnerClass; public class ArrayAlg { //内部类不需要访问外部类对象的时候,应该使用静态内部类 //静态内部类可以有静态域和方法 //声明在接口中的内部类自动成为static和public 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); } }
测试:
package staticInnerClass; 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 pair = ArrayAlg.minmax(d); System.out.println("min = " + pair.getFirst()); System.out.println("max = " + pair.getSecond()); } }