键入带有多个边界的参数 [英] Type parameter with multiple bounds
问题描述
这段代码编译:
import java.io.Serializable;
import java.util.Arrays;
class Test< T扩展了数组&序列化> {}
但是如果我用
替换最后一行 class Test< T extends Serializable&阵列与GT; {}
我得到了此处预期的界面。为什么?
From JLS第4.4节:
每个声明为类型参数的类型变量都有一个绑定。如果没有为类型变量声明绑定,则假定为Object。如果声明了一个绑定,它包含以下任一项:
单个类型变量T或
类或接口类型T可能跟随接口类型I1& ...& In。
如果任何类型I1 ... In是类类型,则是编译时错误或类型变量。
所以基本上,如果你的界限包含一个类,它必须是第一个 (既然 This code compiles: but if I replace the last line with I get "interface expected here". Why? From section 4.4 of the JLS: Every type variable declared as a type parameter has a bound. If no bound is declared for a type variable, Object is assumed. If a bound is declared, it consists of either: a single type variable T, or a class or interface type T possibly followed by interface types I1 & ... & In. It is a compile-time error if any of the types I1 ... In is a class type or type variable. So basically, if your bounds include a class, it has to be the first bound. (Given that 这篇关于键入带有多个边界的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Arrays
不能被实例化,目前还不清楚为什么要 / em>一个包含它的边界,介意你......这仅仅是一个例子吗?)import java.io.Serializable;
import java.util.Arrays;
class Test<T extends Arrays & Serializable> { }
class Test<T extends Serializable & Arrays> { }
Arrays
can't be instantiated, it's unclear why you would want a bound including it, mind you... was this just an example?)