Java泛型(模板)专业化可能(覆盖具有特定类型的模板类型) [英] Java generics (template) specialization possible (overriding template types with specific types)

查看:220
本文介绍了Java泛型(模板)专业化可能(覆盖具有特定类型的模板类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的例子中,我使用了一些特殊类型的特殊类型是一个泛型类(T类型),通常返回null,但返回(空字符串),当T是字符串类型时,或0(零),当它的整数类型等。



仅仅提供方法的类型特定重载会产生方法不明确的错误:

例如:

  public class Hacking {

public static void main(String [] args){
Bar< Integer> barInt = new Bar< Integer>();
Bar< String> barString = new Bar< String>();

//确定,返回null
System.out.println(barInt.get(new Integer(4)));

//错误:方法get(String)对于Bar< String>类型是不明确的。
System.out.println(barString.get(new String(foo)));
}

public static class Bar< T> {

public T get(T x){
return null;
}

public String get(String x){
return;



code $
$ b是子类的唯一选项具有特定类型的泛型类(请参见以下示例中的StringBar?



  public static void main(String [] args){ 
Bar< Integer> barInt = new Bar< Integer>();
StringBar barString2 = new StringBar();

//返回null
System。 out.println(barInt.get());

//确定,返回
System.out.println(barString2.get());
}

public static class Bar< T> {

public T get(){
return null;
}
}

public static class StringBar extends Bar< String> {
public String get(){
return;
}
}
}

这是唯一的方法,必须为每个我想要的类型创建子类都有点痛苦专门用来代替Bar类中get()的重载。



我猜我可以在Bar.get()方法中检查instanceof,例如
T get(T t){
if(t instanceof String)return;
if(t instanceof Integer)返回0;
else返回null;
}



然而,我已经被教导避免使用instanceof并尽可能使用多态。

解决方案

所有事情都考虑到了,共识似乎是问题中提到的StringBar方法是唯一的方法。

  public static class StringBar extends Bar< String> {
public String get(){
return;
}
}


I'm wondering what are the options to specialize generic types in Java, i.e. in a templated class to have specific overrides for certain types.

In my case I was a generic class (of type T) to return null usually, but return "" (the empty string), when T is the String type, or 0 (zero) when its the Integer type, etc.

Merely providing a type-specific overload of a method produces a "method is ambiguous" error:

e.g.:

public class Hacking {

  public static void main(String[] args) {
    Bar<Integer> barInt = new Bar<Integer>();
    Bar<String> barString = new Bar<String>();

    // OK, returns null
    System.out.println(barInt.get(new Integer(4)));

    // ERROR: The method get(String) is ambiguous for the type Bar<String>
    System.out.println(barString.get(new String("foo")));
  }

  public static class Bar<T> {

    public T get(T x) {
      return null;
    }

    public String get(String x) {
      return "";
    }  
  }
}

Is the only option to subclass the generic class with a specific type (see StringBar in the following example?

  public static void main(String[] args) {
    Bar<Integer> barInt = new Bar<Integer>();
    StringBar barString2 = new StringBar();

    // OK, returns null
    System.out.println(barInt.get());

    // OK, returns ""
    System.out.println(barString2.get());
  }

  public static class Bar<T> {

    public T get() {
      return null;
    }
  }

  public static class StringBar extends Bar<String> {
    public String get() {
      return "";
    }
  }
}

Is this is the only way, it's a bit of a pain to have to create a subclass for every type I want to specialize instead of an overload of get() in the Bar class.

I'm guessing I could check the instanceof in the Bar.get() method, e.g. T get(T t) { if (t instanceof String) return ""; if (t instanceof Integer) return 0; else return null; }

However I've been taught to avoid instanceof and use polymorphism when possible.

解决方案

All things considered, the concensus appears to be that the StringBar method method mentioned in the question is the only way to go.

  public static class StringBar extends Bar<String> {
    public String get() {
      return "";
    }
  }

这篇关于Java泛型(模板)专业化可能(覆盖具有特定类型的模板类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆