如何限制Java泛型类型中的允许类型数量 [英] How does one restrict number of allowed types in Java generic type
问题描述
public class GenericType< T> {
private T someVar;
public void setVar(T var){this.someVar = var; }
//其余代码
}
I希望允许它只接受特定类型(字符串/整数/双精度)。我知道有限的通配符,但他们不帮我在这里。在setVar()中,我可以检查 instanceof
并抛出一个异常,如果类型不是整数/字符串等。这是最好的方法吗?
我在做这种类型的操作时遇到同样的问题。根据不同的类型,我想要做不同的操作。对于这种问题,继承和有界通配符似乎是一般的走向,但这些都是原始包装。 使用继承:
Parent.java
/ strong> public abstract class Parent< T> {
public abstract void display(T t);
}
ChildString.java
public class ChildString extends Parent< String> {
@Override
public void display(String t){
//在这里做某事...
}
}
ChildInteger.java
public class ChildInteger extends Parent< Integer> {
@Override
public void display(Integer t){
//在这里做某事...
}
}
ChildDouble.java
public class ChildDouble extends Parent< Double> {
@Override
public void display(Double t){
//在这里做某事...
}
}
并且访问类的子元素,而不是直接访问父类。
更新
另外一个例子:
GenericType.java
public class GenericType {
public void display(Object t){
String msg;
if(t instanceof String){
msg =String;
} else if(t instanceof Integer){
msg =Integer;
} else if(t instanceof Double){
msg =Double;
} else {
msg =另一个对象;
}
System.out.println(msg);
$ p $特定通用.java public class SpecificGeneric {
public static void main(String [] args){
GenericType basicType = new GenericType();
basicType.display(new String());
basicType.display(new Integer(1));
basicType.display(new Double(0.1));
}
}
Say, i have a generic type as below
public class GenericType<T> {
private T someVar;
public void setVar(T var) { this.someVar = var; }
//Rest of the code
}
I want to allow it to take only specific types(String/Integer/Double). I know about bounded wildcards but they don't help me here. In setVar(), I can check the instanceof
and throw an Exception if type is not Integer/String etc. Is this the best way to do it?
I have the same problem when doing operations on this type. Depending on the type, I want to do different operations. Inheritance and bounded wildcards seem like the way to go in general for this kind of problem but these are primitive wrappers.
解决方案 Using Inheritance:
Parent.java
public abstract class Parent<T> {
public abstract void display(T t);
}
ChildString.java
public class ChildString extends Parent<String> {
@Override
public void display(String t) {
// Do something here...
}
}
ChildInteger.java
public class ChildInteger extends Parent<Integer> {
@Override
public void display(Integer t) {
// Do something here...
}
}
ChildDouble.java
public class ChildDouble extends Parent<Double> {
@Override
public void display(Double t) {
// Do something here...
}
}
And access the class child rather than you directly access the parent class.
Update
Here another example:
GenericType.java
public class GenericType {
public void display(Object t) {
String msg;
if(t instanceof String) {
msg = "String";
} else if (t instanceof Integer) {
msg = "Integer";
} else if (t instanceof Double) {
msg = "Double";
} else {
msg = "Another Object";
}
System.out.println(msg);
}
}
SpecificGeneric.java
public class SpecificGeneric {
public static void main(String[] args) {
GenericType basicType = new GenericType();
basicType.display(new String());
basicType.display(new Integer(1));
basicType.display(new Double(0.1));
}
}
这篇关于如何限制Java泛型类型中的允许类型数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!