无法将具有泛型方法的功能接口转换为lambda表达式 [英] Cannot convert functional interface with generic method into lambda expression

查看:87
本文介绍了无法将具有泛型方法的功能接口转换为lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法将具有泛型方法的功能接口转换为lambda表达式.
以下代码正在工作.它没有lambda表达式,即使用匿名类.

Cannot convert functional interface with generic method into lambda expression.
Following code is working. It is without lambda expression i.e. using anonymous class.

public interface Pro{
    public <T> void callee(T t);
}

public void fun(Pro obj){}


public void implement() {
    fun(new Pro() {
        @Override
        public <Integer> void callee(Integer t){}
    });
}

我不明白如何使用lambda表达式代替匿名类.
自己尝试后,我使用了netbeans中显示的提示.我用图示的灯泡来做.

I cannot understand how to use lambda expression instead of anonymous class.
After trying it myself I used the hint shown in netbeans. I used the shown bulb to do it.

这就是我得到的一个错误.

And here is what I got, an ERROR.

public void implement() {
    fun((Integer t) -> {
    });
}

显示错误.在这里使用正确的lambda表达式是什么?
这是错误:

It is showing error. What is the correct lambda expression to be used here?
Here is the error :

one\LambdaScopeTest.java:18: error: incompatible types: invalid functional descriptor for lambda expression
    fun((Integer t) -> {
        ^
method <T>(T)void in interface Pro is generic
where T is a type-variable:
     T extends Object declared in method <T>callee(T)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get
full output

推荐答案

主要问题是您有通用的方法而不是通用的接口.那没有多大意义.改为将接口设为通用后,它就会起作用:

The main problem is that you've got a generic method instead of a generic interface. That doesn't make a lot of sense. As soon as you make the interface generic instead, it works:

@FunctionalInterface
interface Pro<T> {
    void callee(T t);
}

public class Test {    
    public static <T> void fun(Pro<T> obj){}

    public static void main(String[] args) {
        fun((Integer t) -> {});
    }
}

使接口具有通用性更有意义,因为对于不同类型具有不同的实现才有意义.为了使方法通用,建议每个实现都应能够接受任何类型的任何值,因为调用方会指定它-如果您随后将lambda表达式与特定类型.

It makes much more sense for the interface to be generic, as then it makes sense to have different implementations for different types. For the method to be generic suggests that every implementation should be able to accept any value of any type, because the caller would be specifying it - which doesn't make sense if you then use a lambda expression with a specific type.

您的原始版本仅能工作,因为您正在声明一个名为Integer的通用类型参数...您不是Integer类型指定实现.换句话说:

Your original version is only working because you're declaring a generic type parameter called Integer... you're not specifying an implementation for the Integer type. In other words, this:

fun(new Pro() {
    @Override
    public <Integer> void callee(Integer t){}
});

等效于:

fun(new Pro() {
    @Override
    public <T> void callee(T t){}
});

...而且我不知道将那个表示为lambda表达式的方法.基本上,我认为您的原始界面不适用于lambda表达式.

... and I don't know of a way of representing that as a lambda expression. Basically, I think your original interface is inappropriate for lambda expressions.

这篇关于无法将具有泛型方法的功能接口转换为lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆