怎样在调用静态方法时指定泛型类型?

huangapple go评论78阅读模式
英文:

How to specify the generic type when calling a static method?

问题

我有以下的示例:

public class main3
{
  static class Value<T>
  {
    T value;
    Value (T value) { this.value = value; }
  }

  static class IntegerValue extends Value<Integer>
  {
    IntegerValue (Integer value) { super (value); }
    IntegerValue (String value)  { super (Integer.valueOf (value)); }
  }

  static <T> IntegerValue integer (T value) { return new IntegerValue(value); }

  public static void main (String ...argv)
  {
    IntegerValue a = new IntegerValue (42);
    IntegerValue b = new IntegerValue ("42");

    IntegerValue c = integer (42);
    IntegerValue d = integer ("42");
  }
}

这会失败并显示错误:
<!-- language: none -->

main3.java:15: 错误: 找不到适用的构造函数以 IntegerValue(T) 实例化
  static &lt;T&gt; IntegerValue integer (T value) { return new IntegerValue(value); }
                                                     ^
    构造函数 IntegerValue.IntegerValue(Integer) 不适用
      (参数不匹配; 无法将 T 转换为 Integer)
    构造函数 IntegerValue.IntegerValue(String) 不适用
      (参数不匹配; 无法将 T 转换为 String)
  其中 T 是类型变量:
    T extends Object,已在方法 <T>integer(T) 中声明
1 错误

如何在调用泛型 integer 方法时指定正确的 T 类型?
我还尝试了这个:

    IntegerValue c = main3.&lt;Integer&gt;integer (42);
    IntegerValue d = main3.&lt;String&gt;integer (&quot;42&quot;);

但这没有帮助。

英文:

I have the following example:

public class main3
{
  static class Value&lt;T&gt;
  {
    T value;
    Value (T value) { this.value = value; }
  }

  static class IntegerValue extends Value&lt;Integer&gt;
  {
    IntegerValue (Integer value) { super (value); }
    IntegerValue (String value)  { super (Integer.valueOf (value)); }
  }

  static &lt;T&gt; IntegerValue integer (T value) { return new IntegerValue(value); }

  public static void main (String ...argv)
  {
    IntegerValue a = new IntegerValue (42);
    IntegerValue b = new IntegerValue (&quot;42&quot;);

    IntegerValue c = integer (42);
    IntegerValue d = integer (&quot;42&quot;);
  }
}

This fails with the error:
<!-- language: none -->

main3.java:15: error: no suitable constructor found for IntegerValue(T)
  static &lt;T&gt; IntegerValue integer (T value) { return new IntegerValue(value); }
                                                     ^
    constructor IntegerValue.IntegerValue(Integer) is not applicable
      (argument mismatch; T cannot be converted to Integer)
    constructor IntegerValue.IntegerValue(String) is not applicable
      (argument mismatch; T cannot be converted to String)
  where T is a type-variable:
    T extends Object declared in method &lt;T&gt;integer(T)
1 error

How to specify the right type of T when calling the generic integer method?

I tried also this:

    IntegerValue c = main3.&lt;Integer&gt;integer (42);
    IntegerValue d = main3.&lt;String&gt;integer (&quot;42&quot;);

But it does not help.

答案1

得分: 4

只有2个IntegerValue的构造函数可用:IntegerValue(Integer)IntegerValue(String)

因此,你想要的是__不可能的__。理论上你可以编写:

static <T extends Integer> IntegerValue integer(T value) { ... }

但创建一个下界为final类的类型变量显然是毫无用处的。

我猜想你想要的是有一个单一的方法,可以将Integer或String传递给这个方法。

这不是泛型的用途,而且泛型无法实现这一点。

你能够做到的最接近的是:

static IntegerValue integer(String value) { return new IntegerValue(value); }
static IntegerValue integer(Integer value) { return new IntegerValue(value); }
英文:

There are only 2 constructors available for IntegerValue: IntegerValue(Integer) and IntegerValue(String).

Therfore, what you want is not possible. Well, in theory you can write:

static &lt;T extends Integer&gt; IntegerValue integer(T value) { ... }

but creating a typevar whose lower bound is a final class is obviously quite completely useless.

I surmise that what you want is that you have a single method, and that you can pass either an Integer or a String into this method.

That's not what generics are for, and that is not possible with generics.

The closest you can get is:

    static IntegerValue integer(String value) { return new IntegerValue(value); }
    static IntegerValue integer(Integer value) { return new IntegerValue(value); }

huangapple
  • 本文由 发表于 2020年9月4日 21:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63742443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定