功能接口方法参考

时间:2019-03-04 09:52:02

标签: java lambda java-8 functional-interface

我是Java 8的新手,请尝试一下。 我有一个界面

public interface CurrencyRateDao{
    Double getCurrencyRate(String srcCur,String tarCur, int month);
}

以这种方式使用它:

CurrencyRateDao currencyRateDao = new CurrencyRateDaoImpl();
Double rate = ('USD','INR',1) -> currencyRateDao::getCurrencyRate;

出现错误:

  此表达式的

目标类型必须是功能接口。

请提出以上代码的问题

1 个答案:

答案 0 :(得分:3)

您只需要

Double rate = currencyRateDao.getCurrencyRate("USD", "INR", 1);

如果您将接口表示为lambda,则它看起来像:

CurrencyRateDao currencyRateDao = (srcCur, tarCur, month) -> Double.MAX_VALUE;
// accepts three arguments and returns a Double value