当使用 Bounded 类型参数或直接类型接口时
When use Bounded type parameter or type Interface directly
如果您需要将接口类型的参数传递给方法,您可以使用两个 impl。
使用有界类型参数:
public static <I extends InterfaceObj> void isTrue(boolean expression, I interfaceobj) {
if(!expression){
throw new RunTimeException(interfaceobj);
}}
否则你可以使用接口类型:
public static void isTrue(boolean expression, InterfaceObj interfaceobj) {
if(!expression){
throw new RunTimeException(interfaceobj);
}
}
然后,如果我有一个实现 InterfaceObj 的 class,我可以在第一个和第二个示例中使用它,这样我就看不出它们之间的区别、优点和缺点。
- 每种情况有什么区别?
- 什么时候使用一种或另一种更好?
来自甲骨文:
There may be times when you want to restrict the types that can be
used as type arguments in a parameterized type. For example, a method
that operates on numbers might only want to accept instances of Number
or its subclasses. This is what bounded type parameters are for.
在功能上,它完全相同,因为使用接口作为参数类型,它可能也只想接受 Number 或其子实例类。
从技术上讲,它略有不同,因为编译 类 应该不同。
对于您的情况,我更愿意使用不带通配符的原始接口类型,因为它不那么冗长。
这并不意味着方法中的有界类型参数是无用的。
当您使用多个绑定类型参数时,它确实很有用。
假设您的方法接受一个参数,条件是它同时属于两个指定类型:InterfaceObj
和 OtherInterfaceObj
.
对于接口类型,为了满足这一需求,您应该创建另一个接口来扩展这两个接口,并在您的方法中使用它,例如:
public static void isTrue(boolean expression, MyTwoInterfacesObj interfaceobj) {
if (!expression) {
throw new RuntimeException();
}
}
使用多个绑定类型参数,您无需创建额外的接口。您可以指定它,例如:
public static <I extends InterfaceObj & OtherInterfaceObj> void isTrue(boolean expression, I interfaceobj) {
if (!expression) {
throw new RuntimeException();
}
}
有两个接口,限制它们的参数类型有点尴尬,想象一下有三个或四个接口和它们的多种可能混合。
I don't see the difference, advantadges and disadvantages from one or
other.
我想你忘记了 Collections!
如果你有一个 Collection 参数,这就是 有界类型参数 的真正优势发挥作用的地方
在这种方法中,您只能传递一个已实例化的列表,如 List<InterfaceObj> list = new ArrayList<InterfaceObj>();
public static void processList(List<InterfaceObj> input){
//...
}
但如果您使用有界参数化泛型,则可以将以下所有列表作为输入传递
List<InterfaceObj> list = new ArrayList<InterfaceObj>();
List<SubInterfaceObj> list = new ArrayList<SubInterfaceObj>();
List<SubSubInterfaceObj> list = new ArrayList<SubSubInterfaceObj>();
public static void processList(List<? extends InterfaceObj> input){
//...
}
不同之处在于,与第一个或 typed 版本方法中的代码可以知道 传递了哪个 确切的子类型,但是第二个版本不行。
如果您的方法return编辑了与参数类型相同的列表,您会更清楚地注意到差异。版本一可以 return List<I>
- 类型与参数的(子)类型相同的列表,但版本二只能 return 类型为超类型的 List<InterfaceObj>
.
如果您需要将接口类型的参数传递给方法,您可以使用两个 impl。
使用有界类型参数:
public static <I extends InterfaceObj> void isTrue(boolean expression, I interfaceobj) {
if(!expression){
throw new RunTimeException(interfaceobj);
}}
否则你可以使用接口类型:
public static void isTrue(boolean expression, InterfaceObj interfaceobj) {
if(!expression){
throw new RunTimeException(interfaceobj);
}
}
然后,如果我有一个实现 InterfaceObj 的 class,我可以在第一个和第二个示例中使用它,这样我就看不出它们之间的区别、优点和缺点。
- 每种情况有什么区别?
- 什么时候使用一种或另一种更好?
来自甲骨文:
There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.
在功能上,它完全相同,因为使用接口作为参数类型,它可能也只想接受 Number 或其子实例类。
从技术上讲,它略有不同,因为编译 类 应该不同。
对于您的情况,我更愿意使用不带通配符的原始接口类型,因为它不那么冗长。
这并不意味着方法中的有界类型参数是无用的。
当您使用多个绑定类型参数时,它确实很有用。
假设您的方法接受一个参数,条件是它同时属于两个指定类型:InterfaceObj
和 OtherInterfaceObj
.
对于接口类型,为了满足这一需求,您应该创建另一个接口来扩展这两个接口,并在您的方法中使用它,例如:
public static void isTrue(boolean expression, MyTwoInterfacesObj interfaceobj) {
if (!expression) {
throw new RuntimeException();
}
}
使用多个绑定类型参数,您无需创建额外的接口。您可以指定它,例如:
public static <I extends InterfaceObj & OtherInterfaceObj> void isTrue(boolean expression, I interfaceobj) {
if (!expression) {
throw new RuntimeException();
}
}
有两个接口,限制它们的参数类型有点尴尬,想象一下有三个或四个接口和它们的多种可能混合。
I don't see the difference, advantadges and disadvantages from one or other.
我想你忘记了 Collections!
如果你有一个 Collection 参数,这就是 有界类型参数 的真正优势发挥作用的地方
在这种方法中,您只能传递一个已实例化的列表,如 List<InterfaceObj> list = new ArrayList<InterfaceObj>();
public static void processList(List<InterfaceObj> input){
//...
}
但如果您使用有界参数化泛型,则可以将以下所有列表作为输入传递
List<InterfaceObj> list = new ArrayList<InterfaceObj>();
List<SubInterfaceObj> list = new ArrayList<SubInterfaceObj>();
List<SubSubInterfaceObj> list = new ArrayList<SubSubInterfaceObj>();
public static void processList(List<? extends InterfaceObj> input){
//...
}
不同之处在于,与第一个或 typed 版本方法中的代码可以知道 传递了哪个 确切的子类型,但是第二个版本不行。
如果您的方法return编辑了与参数类型相同的列表,您会更清楚地注意到差异。版本一可以 return List<I>
- 类型与参数的(子)类型相同的列表,但版本二只能 return 类型为超类型的 List<InterfaceObj>
.