此表达式的目标类型必须是扩展谓词的功能接口?

时间:2017-06-09 07:53:54

标签: generics java-8 predicate

abstract class QuadPredicate<T,R, U,V> implements Predicate<T> {

    public abstract boolean test(final T t, final R r, final U u,final V v );

    public boolean test(Object t) {
            throw new UnsupportedOperationException();
    }

}

为什么以下语句给我此表达式的目标类型必须是功能接口

QuadPredicate<String ,String , String ,String > q = (String i, String j, String k,String l) -> {return i.equals(j);};

但是以下版本有效:

QuadPredicate<Object ,Object , String ,String > q = 
                (QuadPredicate<Object, Object, String,String>)new ScriptEngineManager().getEngineByName("nashorn").eval(
                "new com.test.QuadPredicate(function(obj, obj2, obj3,obj4) obj.equals(obj2))");

System.out.println(">>>>" + q.test("A", "A", null, null)); //gives true

1 个答案:

答案 0 :(得分:1)

那应该是一个接口,而不是一个类:

@FunctionalInterface
interface QuadPredicate<T, R, U, V> extends Predicate<T> {

    public abstract boolean test(final T t, final R r, final U u, final V v);

    @Override
    default boolean test(T t) {
        throw new UnsupportedOperationException();
    }

}