最近我在阅读 Spring 框架的源代码,有些东西我看不懂:
public Member getMember() {
// NOTE: no ternary expression to retain JDK <8 compatibility even when using
// the JDK 8 compiler (potentially selecting java.lang.reflect.Executable
// as common type, with that new base class not available on older JDKs)
if (this.method != null) {
return this.method;
}
else {
return this.constructor;
}
}
这个方法是类 org.springframework.core.MethodParameter
的一个成员。代码很容易理解,而注释很难理解。
注意: 即使在使用 JDK8编译器时,也没有三元表达式来保持 JDK < 8的兼容性(可能选择
java.lang.reflect.Executable
作为公共类型,因为旧的 JDK 上没有新的基类)
在这种情况下,使用三元表达式和使用 if...else...
构造有什么区别?