为什么允许BiConsumer分配仅接受单个参数的函数? [英] Why is BiConsumer allowed to be assigned with a function that only accepts a single parameter?

查看:626
本文介绍了为什么允许BiConsumer分配仅接受单个参数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下示例,说:

public class MyConsumer {
    public void accept(int i) {}
    public static void biAccept(MyConsumer mc, int i) {}
}

public class BiConsumerDemo {

    public void accept(int i) { }
    public static void biAccept(MyConsumer mc, int i) { }

    private void testBiConsume() {
        BiConsumer<MyConsumer, Integer> accumulator = (x, y) -> {}; // no problem, method accepts 2 parameters
        accumulator = MyConsumer::accept; // accepts only one parameter and yet is treated as BiConsumer
        accumulator = MyConsumer::biAccept; // needed to be static
        accumulator = BiConsumerDemo::accept; // compilation error, only accepts single parameter
    }
}

为什么变量accumulator是需要函数才能接受2个参数的BiConsumer变量,而当该方法仅接受单个参数时,为什么可以用MyConsumer::accept赋值呢?

Why is that the variable accumulator, which is a BiConsumer that requires a function to accept 2 parameters, can be assigned with MyConsumer::accept when that method only accepts only a single parameter?

Java语言设计背后的原理是什么?如果有一个名词,那是什么?

What is the principle behind this language design in Java? If there's a term for it, what is it?

推荐答案

MyConsumer::accept是对MyConsumer类实例方法的引用,该实例方法具有单个参数int.在其上调用该方法的MyConsumer实例被视为方法引用的隐式参数.

MyConsumer::accept is a method reference to an instance method of MyConsumer class that has a single argument of type int. The MyConsumer instance on which the method is called is considered an implicit argument of the method reference.

因此:

accumulator = MyConsumer::accept;

等效于:

accumulator = (MyConsumer m,Integer i) -> m.accept(i);

使用具有 n个参数的目标功能类型,将识别出一组可能适用的方法:

given a targeted function type with n parameters, a set of potentially applicable methods is identified:

  • 如果方法引用表达式的形式为ReferenceType :: [TypeArguments]标识符,则可能适用的方法是要搜索的类型的成员方法,它们具有适当的名称(由Identifier赋予),可访问性,Arity (n或n-1),并按§15.12.2.1中的规定键入参数arity(从[TypeArguments]派生).

  • If the method reference expression has the form ReferenceType :: [TypeArguments] Identifier, the potentially applicable methods are the member methods of the type to search that have an appropriate name (given by Identifier), accessibility, arity (n or n-1), and type argument arity (derived from [TypeArguments]), as specified in §15.12.2.1.

考虑了两个不同的Ar,n和n-1,以说明这种形式是指静态方法还是实例方法.

(摘自 15.13. 1.方法引用的编译时声明)

在您的情况下,目标函数类型是BiConsumer,它具有2个参数.因此,将考虑与名称accept相匹配且具有2个或1个参数的MyConsumer类的任何方法.

In your case the target function type is BiConsumer, which has 2 parameters. Therefore any methods of the MyConsumer class matching the name accept and having 2 or 1 parameters are considered.

具有2个参数的静态方法和具有1个参数的实例方法都可以与目标函数类型匹配.

Both static methods having 2 parameters and instance methods having 1 parameter can match the target function type.

这篇关于为什么允许BiConsumer分配仅接受单个参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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