[英]How to refactor a method to a generic one accepting only a subset of types?
[英]How do I restrict accepting only one type in my generic method?
我有一个通用函数foo,它接受任何类型并打印出来。
public static <T> T foo(T... arg) {
List<T> foo = Arrays.asList(arg);
for (T t : foo) {
System.out.println(t);
}
return null;
}
如何确保收到的参数只有一种类型。 例如,{1,'a',3}应该无效。 它应该是所有数字或所有字符。 我想接受所有整数或所有字符。
事实上你可以这样做:
static <T extends Comparable<T>> void f(T... args) {
System.out.println(java.util.Arrays.toString(args));
}
public static void main(String[] args) {
// all one type -- all of these compile!
f(1, 2, 3); // prints "[1, 2, 3]"
f('a', 'b', 'c'); // prints "[a, b, c]"
f("a", "b", "c"); // prints "[a, b, c]"
f(1D, 2D, 3D); // prints "[1.0, 2.0, 3.0]"
// this is not preventable
f(1, (int)'a', 3); // prints "[1, 97, 3]"
// mixture of types -- none of these compile!
//f(1, 'a', 3); // compilation error!
//f(1, '2', "3"); // compilation error!
//f("a", "b", 'c'); // compilation error!
//f(1, 2, 3D); // compilation error!
}
这利用了以下事实:
Integer implements Comparable<Integer>
Character implements Comparable<Character>
String implements Comparable<String>
Double implements Comparable<Double>
因此,为了匹配这些类型(以及可能的其他类型),我们将T
绑定如下:
这包括诸如java.util.Date
东西,它implements Comparable<Date>
,以及无数其他类型,但如果你还想允许Integer
和Character
,你可能会做的最好。
然而,做牢记Integer
, Character
, String
,都是Object
,所以其实一帮这些混合在一起的是一种类型的列表: Object
。
值得庆幸的是, 并非 Object implements Comparable<Object>
; 否则上述解决方案将无效。
T
部分意味着所有的args
都是相同的类型。
如果要将泛型类型限制为仅某种类型或子类型(例如整数),则可以执行以下操作: -
public static <T extends Integer> T foo(T... arg) {
List<T> foo = Arrays.asList(arg);
for (T t : foo) {
System.out.println(t);
}
return null;
}
我不是一个java开发人员,但你可以做的一个可能的选择是使用类型为T的对象的泛型集合。
public static <T> T foo(List<T> arg) {
List<T> foo = arg;
for (T t : foo) {
System.out.println(t);
}
return null;
}
你可以这样做你想做的事情:
YourClass.<Type>foo(params);
特别:
YourClass.<Integer>foo(1, 2, 3);
和
YourClass.<Character>foo('a', 'b', 'c');
您可以利用foo
返回与输入参数相同类型<T>
的事实。
您可以通过定义返回类型来推断<T>
:
Integer i1 = 4;
String s = "string";
final Integer i2 = foo(i1, s); // error, only Integer allowed
如果未指定返回类型,则类型<T>
将被推断为Object
,因此将接受所有子类。
或者,正如@Finbarr所提到的,您可以通过推断来推断类型
Foo.<Integer>foo(i1, s); // error, only Integer allowed
要声明有界类型参数,请列出类型参数的名称,然后是extends关键字,后跟其上限。
以下方法仅接受数字作为参数。
public static <T extends Comparable<T>> T maximum(T firstNumber, T secondNumber)
{
system.out.println(secondNumber.compareTo(firstNumber));
}
如果不使用Comparable扩展它,则compareTo()
将不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.