java从内部类到匿名类到Lambda表达式简化

首先讲一讲内部类

内部类是在一个已有的类里面创建一个新的类。
就比如在一个TalkingClock中间创建一个TimePrinter的类,
Time Printer 类我后面会用在 TImer里面当做一个Event。Timer的作用是每过多少毫秒执行一次。

import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*;
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()
   {	
   	  TimePrinter Listener=new TimePrinter();
      Timer timer = new Timer(interval, Listener);
      timer.start();
   }
   public class TimePrinter implements ActionListener
   {
   		public void actionPerformed(ActionEvent event)
   		{
        System.out.println("At the tone, the time is " 
           + Instant.ofEpochMilli(event.getWhen()));
        if (beep) Toolkit.getDefaultToolkit().beep();
     }
   }

   
};

这个报时器会在隔一段时间响一下。

匿名类

我们很容易就能看到TimePrinter只用了一次,呢么我们就可以把这个类匿名
具体方法就是把timePrinter这个类的定义写在赋值语句里面。就如下面的这个代码,唯一需要的就是把接口写在这个赋值语句的最上面。

import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*;
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 ActionListener()
   	 {
        System.out.println("At the tone, the time is " 
           + Instant.ofEpochMilli(event.getWhen()));
        if (beep) Toolkit.getDefaultToolkit().beep();
     };
      Timer timer = new Timer(interval, Listener);
      timer.start();
   }
}

最后就是用Lambda表达式

因为Timer()中间使用的是一个时间段(interval)和一个ActionLIstener,所以我们就可以改写这个为Lambda表达式。

import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*;
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 ActionListener()
   	 {
        System.out.println("At the tone, the time is " 
           + Instant.ofEpochMilli(event.getWhen()));
        if (beep) Toolkit.getDefaultToolkit().beep();
     };
      Timer timer = new Timer(interval,event->
   	 {
        System.out.println("At the tone, the time is " 
           + Instant.ofEpochMilli(event.getWhen()));
        if (beep) Toolkit.getDefaultToolkit().beep();
     });
      timer.start();
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值