对 java 规范中的泛型子类型感到困惑?
Confusion over generics subtyping in java specs?
我正在阅读 java 规格
https://docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.10.2
这一行让我感到困惑:
D<U1 θ,...,Uk θ>
, where D<U1,...,Uk>
is a generic type which is a
direct supertype of the generic type C and θ is the
substitution [F1:=T1,...,Fn:=Tn].
Uk θ
是在选角吗?替换后 D
变为 D<(U1)T1,...,(Uk)Tk>
?如果是这样,为什么作者在 Uk θ
中省略括号作为转换语法的一部分?谢谢!
没有铸造。它是用于描述类型的数学语言。
在代码中,如果:
interface D<A, B> {}
interface C<A, B> extends D<A, B> {}
class Y implements D<String, Number> {}
class X implements C<String, Integer> {} // Note Integer is a subtype of Number
然后编译:
D d = new X();
Given a generic type declaration C<F1,...,Fn>
(n > 0), the direct
supertypes of the parameterized type C<T1,...,Tn>
, where Ti (1 ≤ i ≤
n) is a type, are all of the following:
D<U1 θ,...,Uk θ>
, where D<U1,...,Uk>
is a generic type which is a
direct supertype of the generic type C<F1,...,Fn>
and θ is the
substitution [F1:=T1,...,Fn:=Tn].
让我们看一个例子。
public class D<U1,U2> {
}
//Note the order of F3,F4.
class C<F1,F2,F3,F4> extends D<F4,F3>{
}
class Main{
public static void main(String[] args) {
C<Integer,String,Float,Number> obj = new C<>();
D<Float ,Number> obj2 = obj; // Compilation error
D<Number ,Float> obj3 = obj; //This is fine
}
}
你认为 θ
是什么?
θ
= [F1:= 整数, F2:= 字符串, F3:= 浮点数, F4:= 数字]
现在 D<Number,Float>
是正确的超类型。但不是 D<Float,Number>
。为什么?
因为 Ui θ
。那么这是什么意思?
嗯,它只是意味着替代,即扫描 U1 , U2 , ... till Uk
,如果找到任何 Fi
,则将其替换为 Ti
。
在我们的示例中,它表示扫描 class D 的 F4
和 F3
。您是否在 θ 中有 F4
和 F3
?是的。
那么 F4
,F3
of D
必须是 定义在 θ
.
注 : 按照我的理解回答。我没有具体参考 Uk θ
在 Java 类型语义中的含义。
我正在阅读 java 规格 https://docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.10.2 这一行让我感到困惑:
D<U1 θ,...,Uk θ>
, whereD<U1,...,Uk>
is a generic type which is a direct supertype of the generic type C and θ is the substitution [F1:=T1,...,Fn:=Tn].
Uk θ
是在选角吗?替换后 D
变为 D<(U1)T1,...,(Uk)Tk>
?如果是这样,为什么作者在 Uk θ
中省略括号作为转换语法的一部分?谢谢!
没有铸造。它是用于描述类型的数学语言。
在代码中,如果:
interface D<A, B> {}
interface C<A, B> extends D<A, B> {}
class Y implements D<String, Number> {}
class X implements C<String, Integer> {} // Note Integer is a subtype of Number
然后编译:
D d = new X();
Given a generic type declaration
C<F1,...,Fn>
(n > 0), the direct supertypes of the parameterized typeC<T1,...,Tn>
, where Ti (1 ≤ i ≤ n) is a type, are all of the following:
D<U1 θ,...,Uk θ>
, whereD<U1,...,Uk>
is a generic type which is a direct supertype of the generic typeC<F1,...,Fn>
and θ is the substitution [F1:=T1,...,Fn:=Tn].
让我们看一个例子。
public class D<U1,U2> {
}
//Note the order of F3,F4.
class C<F1,F2,F3,F4> extends D<F4,F3>{
}
class Main{
public static void main(String[] args) {
C<Integer,String,Float,Number> obj = new C<>();
D<Float ,Number> obj2 = obj; // Compilation error
D<Number ,Float> obj3 = obj; //This is fine
}
}
你认为 θ
是什么?
θ
= [F1:= 整数, F2:= 字符串, F3:= 浮点数, F4:= 数字]
现在 D<Number,Float>
是正确的超类型。但不是 D<Float,Number>
。为什么?
因为 Ui θ
。那么这是什么意思?
嗯,它只是意味着替代,即扫描 U1 , U2 , ... till Uk
,如果找到任何 Fi
,则将其替换为 Ti
。
在我们的示例中,它表示扫描 class D 的 F4
和 F3
。您是否在 θ 中有 F4
和 F3
?是的。
那么 F4
,F3
of D
必须是 定义在 θ
.
注 : 按照我的理解回答。我没有具体参考 Uk θ
在 Java 类型语义中的含义。