首页 > 解决方案 > 如何用接口方法的方法引用替换 lambda?

问题描述

如何用接口方法中的方法引用替换 lambda?

public abstract interface Job
{
  public abstract String empName();

  public void JobWithDesc(String title)
  {
    setJobValueDesc(title, empName());
  }
}



public final class JobClient {

  private static final Job job;


  static {
    job = () -> ListJob.getName;
  }

  public static final Job getJob() {
    return job;
    }
}

想用引用替换这个 (job = () -> ListJob.getName;) lambda。请帮助我

标签: javajava-8

解决方案


job = "EmpApplication"::toString;

The question, though, is why you want to turn it into a method reference.

It's a simple lambda returning a String literal. There is no method being called.

We could wrap "EmpApplication" into a static method and refer to it or use an instance method that returns itself.


推荐阅读