当名义 class 需要访问修改时无法使用交集类型
unable to use an intersection type when notional class requires access modification
接口:
interface PublicCloneable {
Object clone();
}
interface HasPosition {
// doesn't matter
}
尝试使用交集类型:
@SuppressWarnings("unchecked")
<E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
final E clone = (E)elem.clone();
// rest omitted
}
尝试用javac 1.8.0_60
编译:
$ javac xx.java
xx.java:13: error: clone() in Object cannot implement clone() in PublicCloneable
<E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
^
attempting to assign weaker access privileges; was public
xx.java:14: error: clone() has protected access in Object
final E clone = (E)elem.clone();
^
2 errors
为什么这个交集类型对 javac 无效?
正如第一个错误试图告诉您的那样,您的代码无法工作,因为您限制了 clone()
方法具有两个不兼容签名的类型。
这看起来像一个 javac 错误。
http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.4
The members of a type variable X with bound T & I1 & ... & In are the members of the intersection type (§4.9) T & I1 & ... & In
http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.9
Every intersection type T1 & ... & Tn induces a notional class or interface for the purpose of identifying the members of the intersection type ...
If Ck is Object, a notional interface is induced ... has direct superinterfaces T1', ..., Tn'
因此,PublicCloneable & HasPosition
引入了一个概念接口,扩展了两者,应该可以。
接口:
interface PublicCloneable {
Object clone();
}
interface HasPosition {
// doesn't matter
}
尝试使用交集类型:
@SuppressWarnings("unchecked")
<E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
final E clone = (E)elem.clone();
// rest omitted
}
尝试用javac 1.8.0_60
编译:
$ javac xx.java
xx.java:13: error: clone() in Object cannot implement clone() in PublicCloneable
<E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
^
attempting to assign weaker access privileges; was public
xx.java:14: error: clone() has protected access in Object
final E clone = (E)elem.clone();
^
2 errors
为什么这个交集类型对 javac 无效?
正如第一个错误试图告诉您的那样,您的代码无法工作,因为您限制了 clone()
方法具有两个不兼容签名的类型。
这看起来像一个 javac 错误。
http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.4
The members of a type variable X with bound T & I1 & ... & In are the members of the intersection type (§4.9) T & I1 & ... & In
http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.9
Every intersection type T1 & ... & Tn induces a notional class or interface for the purpose of identifying the members of the intersection type ...
If Ck is Object, a notional interface is induced ... has direct superinterfaces T1', ..., Tn'
因此,PublicCloneable & HasPosition
引入了一个概念接口,扩展了两者,应该可以。