如何修复模糊方法参考?

时间:2017-02-14 07:09:16

标签: java java-8

如何解决班级Test中的两条错误行而不修改班级Case

class Case {
    public boolean isEmpty() {
        return true;
    }

    public static boolean isEmpty(Case t) {
        return true;
    }
}

public class Test {
    public static void main(String[] args) {
        Predicate<Case> forNonStaticMethod = Case::isEmpty;//<--TODO: fix compile error: 
        Predicate<Case> forStaticMethod = Case::isEmpty;//<--TODO: fix compile error: 
        //Ambiguous method reference: both isEmpty() and isEmpty(Case) from the type Case are eligible          
    }
}

1 个答案:

答案 0 :(得分:4)

您可以使用lambda表达式而不是方法引用:

Predicate<Case> forNonStaticMethod = c -> c.isEmpty();
Predicate<Case> forStaticMethod = c -> Case.isEmpty(c);