<CoreJava> 6.4 内部类
- package com.corejava.innerclass;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.Timer;
- /**
- *
- * @author vincent
- *
- */
- public class InnerClassTest {
- public static void main(String[] args) {
- TalkingClock clock = new TalkingClock(1000, true);
- clock.start();
- JOptionPane.showMessageDialog(null, "Quit program?");
- System.exit(0);
- }
- }
- 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 t = new Timer(interval, listener);
- t.start();
- }
- public class TimePrinter implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- Date now = new Date();
- System.out.println("At the tone, the time is " + now);
- if(beep)
- Toolkit.getDefaultToolkit().beep();
- }
- }
- }
- public void start() {
- class TimerPrinter implements 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();
- }
- }
- ActionListener listener = new TimerPrinter();
- Timer t = new Timer(interval, listener);
- t.start();
- }
- public void start(int interval, final boolean beep) {
- class TimerPrinter implements 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();
- }
- }
- ActionListener listener = new TimerPrinter();
- Timer t = new Timer(interval, listener);
- t.start();
- }
6.4.6匿名内部类
1、将局部内部类深入,若只创建这个类的一个对象,就不用命名了。这种类就是匿名内部类。
小程序:
public void start(int interval, final boolean beep) {
ActiongListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
System.out.println("At the tone, time is " + now);
if(beep)
Toolkit.getDefultToolkit().beep();
}
};
Timer t = new Timer(interval, listener);
t.start();
}
代码解析:创建了一个实现ActiongListener接口的类的新对象,这个新对象需要实现actionPerformed所定义的方法。
2、构造器的名字必须和类名相同,但是匿名类没有类名,所以匿名类没有构造器。取而代之的是将构造器的参数传递给超类(父类)构造器。尤其是内部类实现接口是不能有任何构造参数,而且还要提供一组括号,例如:
new InterfaceType() {
methods and data;
}
3、构造一个类的新对象共和构造一个匿名类的区别:
Person queen = new Person("Mary"); //a Person object
Person count = new Person() {....}; //an object of an inner class extending Person
如果构造参数的闭源括号跟一个开花括号,表示正在定义的就是匿名内部类
例6-5源代码:将这个程序与之前的6-4相比较会发现匿名类的解决方案比较简单、实际和易于理解
- package com.core.anonymousinnerclass;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Date;
- import javax.swing.JOptionPane;
- import javax.swing.Timer;
- /**
- *
- * @author vincent
- *
- */
- public class AnonymousInnerClass {
- public static void main(String[] args) {
- TalkingClock clock = new TalkingClock();
- clock.start(1000, true);
- //keep progra running until user selects "OK"
- JOptionPane.showMessageDialog(null, "Quit program?");
- System.exit(0);
- }
- }
- class TalkingClock {
- public void start(int interval, final boolean beep) {
- ActionListener listener = new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- Date now = new Date();
- System.out.println("At the tone, time is: " + now);
- if(beep)
- Toolkit.getDefaultToolkit().beep();
- }
- };
- Timer t = new Timer(interval, listener);
- t.start();
- }
- }
- package com.core.staticinnerclass;
- /**
- *
- * @author vincent
- *
- */
- public class StaticInnerClass {
- 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); //不需要用new生成对象,静态方法可以直接引用
- System.out.println("min = " + p.getFirst());
- System.out.println("max = " + p.getSecond());
- }
- }
- class ArrayAlg {
- //A pair of floating-point numbers
- public static class Pair {
- private double first;
- private double second;
- //Constructs a pair from two floating-point numbers
- public Pair(double f, double s) {
- first = f;
- second = s;
- }
- //Returns the first numbers of the pair
- public double getFirst() {
- return first;
- }
- //Return the second numbers of the pair
- public double getSecond() {
- return second;
- }
- }
- //minmax方法返回 Pair这个静态内部类
- public static Pair minmax(double[] values) {
- double min = Double.MAX_VALUE;
- double max = Double.MIN_VALUE;
- for (double v : values) {
- if (min > v)
- min = v;
- if (max < v)
- max = v;
- }
- return new Pair(min, max);
- }
- }