繁体   English   中英

试图理解为什么两段相似的代码是不同的。 首先编译好。 二不

[英]Trying to understand why two similar piece of code are different. First compiles well. Second does not

// 比较 2 个字符串

BinaryOperator<String> maxLengthString = BinaryOperator.maxBy(
            Comparator.comparingInt(String::length));

// 比较 2 个整数

BinaryOperator<Integer> maxLengthString = BinaryOperator.maxBy(
            Comparator.comparingInt(Integer::compareTo));

String::length取 1 个 String 并返回 1 个 int。

Integer::compareTo接受 2 个整数并返回 1 个整数。

查看方法的签名。

compareTo 方法需要隐式传递一个参数。 在这种情况下,编译器无法检索参数,因此会引发编译时错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM