Java 11(可能无关紧要):
public static String toString(Object obj) {
return ReflectionToStringBuilder.toString(obj, ToStringStyle.SHORT_PREFIX_STYLE);
}
public static String toString(Collection<Object> collection) {
return collection.stream()
.map(SaLogUtils::toString)
.collect(Collectors.joining(", ", "[", "]"));
}
public static void main(String[] args) {
List<Integer> list = List.of(Integer.valueOf(1));
System.out.println(SaLogUtils.toString(list));
System.out.println(SaLogUtils.toString(List.of(Integer.valueOf(1))));
}
令人惊讶的输出:
// from toString(Object)
ImmutableCollections.List12[e0=1,e1=<null>]
// from toString(Collection<Object>)
[Integer[value=1]]
为什么Java静态选择不同的方法?
最佳答案
当可以调用多个重载时,Java 选择 the most specific applicable method :
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error. In cases such as an explicitly typed lambda expression argument (§15.27.1) or a variable arity invocation (§15.12.2.4), some flexibility is allowed to adapt one signature to the other.
toString(Collection<Object>)
不适用于 List<Integer>
, 因为 a List<Integer>
isn't a List<Object>
,所以它不是 Collection<Object>
任何一个。因此,只有 toString(Object)
方法是适用的,所以这就是被调用的方法。toString(Collection<Object>)
适用于 List.of(someInteger)
因为那 List.of
是一个多表达式:它可能是 List<Integer>
,可能是 List<Object>
,可能是 List<Serializable>
.由于两者
toString(Object)
和 toString(Collection<Object>)
是适用的,它必须选择其中之一(或声明不明确)。 Collection
重载更具体,因为:toString(Collection<Object>)
的任何信息也可转至toString(Object)
toString(Object)
不能传递给 toString(Collection<Object>)
(例如 new Object()
)。 这使得
toString(Collection<Object>)
更具体,所以这是一个被选择的。
关于Java静态多态(重载)和泛型之间的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60059066/