zoukankan      html  css  js  c++  java
  • lambda和抽象类

    lambda的使用条件是‘一个接口仅有一个待实现的方法’;

    so,lambda不能使用在抽象类上,使用后或提示‘Target type of a lambda conversion must be an interface

    非要使用,需要变通;

    例如,抽象类 TimerTask

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        System.out.println("99999");
    }, 1000);

    在这里用lambda的话就会报错,但可以把Timer拓展一下

    public class MyTimer extends Timer {
     
        public TimerTask schedule(final Runnable r, long delay) {
            final TimerTask task = new TimerTask() { public void run() { r.run(); }};
            this.schedule(task, delay);
            return task;
        }
    }

    然后使用MyTimer

    MyTimer timer = new MyTimer();
    timer.schedule(()->{
       System.out.println("99999");
    }, 1000); 
    timer.schedule(
    this::run, 1000);
  • 相关阅读:
    04.
    24
    39
    46
    72.
    21.
    logout: not found”
    Username is not in the sudoers file. This incident will be reported
    激活函数
    排序算法
  • 原文地址:https://www.cnblogs.com/chenglc/p/9158949.html
Copyright © 2011-2022 走看看