编程语言
首页 > 编程语言> > Java内部类

Java内部类

作者:互联网

1、使用内部类访问对象状态

类:

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);
    }
}

2、内部类的特殊语法规则

  1. 使用外围类引用的正规语法:OuterClass.this
    例:if (TalkingClock.this.beep)
  2. 编写内部对象构造器:outerObject.new InnerClass(construction parameters)
    例:ActionListener listener = this.new TimerPrinter();
  3. 在外围类的作用域之外引用内部类:OuterClass.InnerClass
  4. 内部类中声明的所有静态域都必须是final;

3、局部内部类

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();
    }
}

4、由外部方法访问变量

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();
    }
}

5、匿名内部类

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();
    }
}

6、静态内部类

类:

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());
    }
}

标签:Java,内部,java,ActionListener,beep,new,import,public
来源: https://blog.csdn.net/shyshyshyshy1/article/details/120284168