且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java lambda可以有多个参数吗?

更新时间:2023-11-14 20:39:46

如果您定义具有多个类型参数的功能接口,则可以。没有这种内置类型。 (有一些带有多个参数的有限类型。)

It's possible if you define such a functional interface with multiple type parameters. There is no such built in type. (There are a few limited types with multiple parameters.)

@FunctionalInterface
interface Function<One, Two, Three, Four, Five, Six> {
    public Six apply(One one, Two two, Three three, Four four, Five five);
}

public static void main(String[] args) throws Exception {
    Function<String, Integer, Double, Void, List<Float>, Character> func = (a, b, c, d, e) -> 'z';
}






也没办法定义一个可变数量的类型参数,如果这是你要问的。


There's also no way to define a variable number of type parameters, if that's what you were asking about.

某些语言,如Scala,定义了内置此类型的数量,具有1,2,3,4,5,6等类型参数。

Some languages, like Scala, define a number of built in such types, with 1, 2, 3, 4, 5, 6, etc. type parameters.