English 中文(简体)
如何限制 Java通用类允许的种类
原标题:How does one restrict number of allowed types in Java generic type

2. 具有以下通用类型:

public class GenericType<T> {
   private T someVar;

   public void setVar(T var) { this.someVar = var; }

   //Rest of the code
}

我想允许它只采用特定类型(String/Integer/Double)。 我知道有约束的野心,但他们没有帮助我。 在设计书上,我可以检查<编码>instanceof,如果类型不是 Integer/String等,就可提出例外。 这样做的最佳方式吗?

我在开展此类行动时也存在同样的问题。 根据这种类型,我想采取不同的行动。 肥胖和受约束的野心似乎像对待此类问题的一般方式一样,但这些是原始的结晶。

最佳回答

利用遗产:

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...
    }

}

并且与孩子接触,而不是直接接触父母。

Update

还有一个例子:

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));
    }
}
问题回答

You cannot (more than extends something, but in your case you want few unrelated types, so it does not help).

What you can, is check instance passed to method (you already know it). If you want one instace of generic class for eg. String another for Integers, but don t allow eg. Point2D, you can make constructor with parameter Class clazz and check when constructing whether its allowed. If you are more paranoid, you can store that clazz and in all function compare whether parameter is actualy that class.

这样,你就能够创造我的地图集,但不能以此为榜样。 (你可以 cast取,用不光彩的证据)

在必要是最快捷和最容易的选择的情况下,请见<代码>GenericType<Double>和instanceof。 或者超载setVar(.),接受您通用类别的限制。

  public static class GenericType<T>
  {
    private T someVar;
    public void setVar(String var)
    {
      this.someVar = (T) var;
    }

    public void setVar(Integer var)
    {
      this.someVar = (T) var;
    }

    public void setVar(Double var)
    {
      this.someVar = (T) var;
    }
  }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签