- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的代码无法使用 JDK 7 进行编译,但可以使用 JDK 8 进行编译。
抽象实际代码:
interface A {
...
}
class B implements A {
...
}
public void AAA(List<A> list) {...}
AAA(Collections.singletonList(new B()));
Collections.singletonList 定义为
public static <T> List<T> singletonList(T o) {
return new SingletonList<>(o);
}
据我所知,基于泛型,T 将被推断为 B,因此 Collections.singletonList(new B()) 将是无法分配给 List 的列表,因为 Java 泛型是不变的。
但是对于 JDK 8,T 被推断为 A 并且编译成功。
我想知道如何将 T 推断为 A,因为这里有两个类型 T 的变量:A 和 B。
有先后顺序吗?还是编译器找到共同的祖先类?
附上官方文档更胜一筹!
提前致谢!
ps1。 JDK 7版本为Oracle 1.7.0_79,JDK 8版本为Oracle 1.8.0_66。
PS2。以下是实际代码的链接:
最佳答案
嗯,有一个全新的章节,§18. Type Inference , 在语言规范中,但它并不容易阅读。即使是第一部分的摘要(准确地解决您的问题)也很难:
In comparison to the Java SE 7 Edition of The Java® Language Specification, important changes to inference include:
- Adding support for lambda expressions and method references as method invocation arguments.
- Generalizing to define inference in terms of poly expressions, which may not have well-defined types until after inference is complete. This has the notable effect of improving inference for nested generic method and diamond constructor invocations.
- Describing how inference is used to handle wildcard-parameterized functional interface target types and most specific method analysis.
- Clarifying the distinction between invocation applicability testing (which involves only the invocation arguments) and invocation type inference (which incorporates a target type).
- Delaying resolution of all inference variables, even those with lower bounds, until invocation type inference, in order to get better results.
- Improving inference behavior for interdependent (or self-dependent) variables.
- Eliminating bugs and potential sources of confusion. This revision more carefully and precisely handles the distinction between specific conversion contexts and subtyping, and describes reduction by paralleling the corresponding non-inference relations. Where there are intentional departures from the non-inference relations, these are explicitly identified as such.
- Laying a foundation for future evolution: enhancements to or new applications of inference will be easier to integrate into the specification.
第二个项目符号对您的代码示例影响最大。你有一个泛型方法的嵌套方法调用,没有指定显式类型参数,这使它成为一个所谓的聚合表达式,其实际类型可以从目标类型推断出来,这是AAA
的参数类型在你的情况下。
所以这是一个相当简单的星座 AAA
不是通用的,关于它的参数类型没有歧义。总是List<A>
.这里没有搜索“公共(public)祖先类”,所有需要检查的是参数表达式的类型 ( B
) 是否与推断类型 ( A
) 兼容。
关于java - JDK 8 的类型推断如何与泛型一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41744852/
我是一名优秀的程序员,十分优秀!