zoukankan      html  css  js  c++  java
  • java 核心技术卷一笔记 6 .2接口 lambda 表达式 内部类

    6.2 接口实例

    6.2.1  接口与回调

    在java.swing包中有一个Timer类,可以使用它在到达给定的时间间隔时发出通告,假如程序中有一个时钟,就可以请求每秒钟获得一个通告,以便更新时钟的表盘。

    在构造定时器时,需要设置一个时间间隔,并告知定时器,当到达时间间隔时需要做些什么操作,(java将某个类的对象传递给定时器,然后的定时器调用这个对象的方法。)-----定时器需要知道调用了哪一个方法,并要求传递的对象所属的的类实现了java.awt.event包的ActionListener接口。

    1 public interface ActionListener
    2 {
    3     void actionPerformed(ActionEvent event);
    4 }

    当到达指定的时间间隔时,定时器就调用actionPerformed方法。

    接下来,构造这个类的一个对象,并将他传递给Timer构造器。

    1 ActionListener listener =new TimePrinter();
    2 Timer t =new Timer(10000,listener);

    下面程序给出了定时器和监听器的操作行为,在定时器启动以后,程序将弹出一个消息对话框,并等待用户点击OK按钮来终止程序的运行。定时时间为5秒;

     1 package cc.openhome;
     2 import java.awt.Toolkit;
     3 import java.awt.event.ActionEvent;
     4 import java.awt.event.ActionListener;
     5 import java.util.Date;
     6 import javax.swing.JOptionPane;
     7 import javax.swing.Timer;
     8 public class TimerTest {
     9     public static void main(String[] args) {
    10         ActionListener listener =new TimePrinter();
    11         Timer t=new Timer(5000,listener);
    12         t.start();
    13         JOptionPane.showMessageDialog(null, "Quit program?");
    14         System.exit(0);
    15     } 
    16 }
    17 class TimePrinter implements ActionListener
    18 {
    19     public void actionPerformed(ActionEvent evet )
    20     {
    21         System.out.println("At the tone ,the time is "+new Date());
    22         Toolkit.getDefaultToolkit().beep();
    23     }
    24 }
    At the tone ,the time is Sun Mar 18 10:28:51 CST 2018
    At the tone ,the time is Sun Mar 18 10:28:56 CST 2018
    At the tone ,the time is Sun Mar 18 10:29:01 CST 2018
    At the tone ,the time is Sun Mar 18 10:29:06 CST 2018
    At the tone ,the time is Sun Mar 18 10:29:11 CST 2018
    成功构建 (总时间: 26 秒)
    run:

    Api    javax.swing.JOptionPane 1.2

            static void showMessageDialog(ComPonent parent,Object message)

           显示一个包含一条消息和OK按钮的对话框,这个对话框将位于其parent组件的中央。

    API javax.swing..Timer 1.2

        Timer(int interval ,ActionListener Listener)

       构造一个定时器,每隔interval毫秒通告listener一次。

        void start()

    启动定时器。一旦启动成功,定时器将调用监听器的actionPerformed。

    API void stop()

           停止定时器,一旦停止成功,定时器将不在调用监听器的actionPerformed。

    java。awt.Toolkit  1.0

       static  Toolkit getDefaultToolkit()

        获得默认的工具箱。工具箱包含有关GUI环境的信息。

        void beep()

       发出一声铃响。

    6.2.2  Comparator接口

    假设我们希望按长度递增的顺序对字符串进行排序,而不是按字典顺序进行排序。

    处理这种情况,Arrays.sort方法还有第二个版本,有一个数组和一个比较器(comparator)作为参数,比较器实现了Comparator接口的类的实例。

    public interface Comparator<T>
    {
        int compara(T first,second);
    }

    要按长度比较字符串,可以如下定义一个实现Comparator<String>的类

    class LengthComparator implements Comparator<String>
    {
        public int compare(String first,String second)
        {
            return first.length()-second.length();
        }
    }

    具体完成比较时,需要建立一个实例:

    Comparator<String> comp =new lengthComparator();
    if (comp.compre(words[i],words[j]>0)...

    要对一个数组排序,需要为Arrays.sort方法传入一个LengthComparator对象:

    String[] friengds ={"Peter","Paul","Mary"};
    Arrays.sort(friends,new LengthComparator());

  • 相关阅读:
    CSS3实现的超酷页面过渡效果
    HTML5基本知识小测验
    多线程揭秘
    【框架设计】泛型的应用
    sql server 2005中的except和INTERSECT运算 .
    C#应用MemoryStream提高File读取速度
    .net错误处理机制
    sql 权限导致的问题
    超时时间已到。在操作完成之前超时时间已过或服务器未响应
    SQL差集的使用(EXCEPT)
  • 原文地址:https://www.cnblogs.com/jinmoon/p/8595911.html
Copyright © 2011-2022 走看看