通过方法参考了解编译时错误 [英] Understand the compile time error with Method Reference

查看:66
本文介绍了通过方法参考了解编译时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,方法参考绝对不是静态调用.它适用于静态和非静态方法. 当我们在给定的类中定义自己的非静态方法并尝试使用方法引用"使用它时,在Function情况下看不到编译时错误无法对非静态方法进行静态引用",而在情况下才看到供应商,消费者和谓词.为什么会这样?

As per the Documentation, Method Reference is absolutely not a static call. It works on both static and non- static methods. When we define our own non-static method in a given class and try to use it using Method Reference then the compile-time-error "cannot make static reference to non static method" is NOT seen in case of Function but only seen in case of Supplier, Consumer and Predicate. Why is that so?

class Demo{
    private Function<Student, Integer> p= Student::getGradeLevel; // fine
    private Supplier<Integer> s = Student::supply; // compile-time error
    private Predicate<Integer> p1= Student::check; //compile-time error
    private Consumer<Integer> c=  Student::consume; / compile-time error
    private Function<String, String> f1 = String::toUpperCase; //fine
}

class Student{
    public int getGradeLevel() {
        return gradeLevel;
    }

    public boolean check(int i) {
        return true;
    }

    public int supply() {
        return 1;
    }

    public void consume(int i) {
        System.out.println(i);
    }
}

推荐答案

您必须同时遵循Student方法的返回类型和形式参数类型,并使用适当的功能接口.

You have to follow both the return type and the formal parameter type of the Student method and use the appropriate functional interface.

private Supplier<Integer> s = Student::supply; // compile-time error

Supplier<T>不消耗任何东西 并返回T.一个例子是:

The Supplier<T> consumes nothing and returns T. An example would be:

Student student = new Student();
Supplier<Integer> s = () -> student.supply();

方法参考Student::supply的相关功能接口为 Function<T, R>.以下两者是相等的:

The relevant functional interface for the method reference Student::supply is Function<T, R>. The both following are equal:

Function<Student, Integer> function = student -> student.supply();
Function<Student, Integer> function = Student::supply;

// You have already used a method reference with the very same return and parameter types
Function<Student, Integer> p = Student::getGradeLevel;


private Predicate<Integer> p1= Student::check; //compile-time error

同样的问题,但Predicate<T> 消费 T并返回Boolean.

The very same issue but Predicate<T> consumes T and returns Boolean.

Student student = new Student();
Predicate<Integer> p =  i -> student.check(i);

如果要使用Student::check方法参考,可以使用BiPredicate<T, R>导致Boolean:

You can use BiPredicate<T, R> that results in Boolean if you want to use Student::check method reference:

BiPredicate<Student, Integer> biPredicate = (student, integer) -> student.check(integer);
BiPredicate<Student, Integer> biPredicate = Student::check;


private Consumer<Integer> c= Student::consume; / compile-time error

什么也没新,Consumer<T>消耗T却不返回 (返回类型为void).

Again nothing new, Consumer<T> consumes T and returns nothing (the return type is void).

Student student = new Student();
Consumer<Integer> c = integer -> student.consume(integer);

方法参考Student::consume适用于同时使用Student和某些IntegerBiConsumer:

The method reference Student::consume is suitable for BiConsumer consuming both Student and some Integer:

BiConsumer<Student, Integer> biConsumer = (student, integer) -> student.consume(integer);
BiConsumer<Student, Integer> biConsumer = Student::consume;

这篇关于通过方法参考了解编译时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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