内部类

来源:互联网 发布:超融合和云计算 编辑:程序博客网 时间:2024/06/06 00:42

使用内部类的原因

1、内部类方法可以访问该类定义所在的作用域中的数据,包括私有的数据

2、内部类可以对同一个包中的其他类隐藏起来

3、当想要定义一个回调函数且不想编写大量代码时,使用匿名(anonymous)内部类比较便捷

内部类知识点

1.内部类自动拥有对外围类所有成员的访问权。

2.如果要创建内部类的对象,如果内部类是静态类,可以采用不创建外围类对象的方式,直接创建内部静态类,否则,都要通过先创建外围类对象,再创建内部类对象的方式来创建该内部类对象

内部类--定义在类中

import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.Timer; /** * This program demonstrates the use of inner classes. */public class Test{     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 i, boolean b){interval = i;beep = b;}public void start(){ActionListener listener = new TimePrinter();Timer t = new Timer(interval, listener);t.start();}private class TimePrinter implements ActionListener //an inner class{public void actionPerformed(ActionEvent event)   {        Date now = new Date();      System.out.println("At the tone, the time is " + now);      if(beep)  //beep是外围类的数据,自动隐式引用可以访问      Toolkit.getDefaultToolkit().beep();   }}}

局部内部类--定义在类的方法中

TimePrinter这个类只在start方法中使用了一次,所以可以只在start方法中定义局部内部类,其对start这个方法之外的所有代码都不可见
import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.Timer; /** * This program demonstrates the use of inner classes. */public class Test{     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 i, boolean b){interval = i;beep = b;}public void start(){class TimePrinter implements ActionListener //start方法中的局部内部类{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 TimePrinter();Timer t = new Timer(interval, listener);t.start();}}

匿名内部类

import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.Timer; /** * This program demonstrates the use of inner classes. */public class Test{     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 i, boolean b){interval = i;beep = b;}public void start(){//匿名内部类,没有类名,创建一个实现ActionListener接口的类的新对象,所实现的方法在{}内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();}}

静态内部类

有时候使用内部类只是为了把一个类隐藏在另外一个类的内部,并不需要内部类引用外围类对象。为此,可以将内部类声明为static,以便取消产生的外围类隐式引用
public class Test{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("min=" + p.getFirst());System.out.println("max=" + p.getSecond());}}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.MAX_VALUE;double max = Double.MIN_VALUE;for(double v: values){if(v < min)min = v;if(v > max)max = v;}//因为遍历一次数组返回需要最大最小两个值,所以返回一个包含有两个数据的类return new Pair(min, max); }}