如何将三个具有不同参数类型的相似方法合并为一个泛型方法?



我对java相对缺乏经验&泛型,如果这是个愚蠢的问题请原谅。

我有3个非常相似的助手方法称为verifyTextualSort, verifyNumericSort和verifyDateSort。

这三个方法遵循相同的模式,只有细微的差别:

private boolean verifyTextualSort(...) {
    ArrayList<String> list = new ArrayList<String>();
    // Do common stuff with the list
    // Do textual-specific stuff 
    // Do common stuff with the list
}
private boolean verifyNumericSort(...) {
    ArrayList<Integer> list = new ArrayList<Integer>();
    // Do common stuff with the list
    // Do Numeric-specific stuff
    // Do common stuff with the list
}

是否有某种方法可以将它们组合成一个方法,以某种方式传递类型(Integer, String, Date)作为参数?我必须能够从方法内部知道哪个是类型这样我才能做正确的特定的事情

您需要三个方法来处理特定的内容。然而,对于常见的东西,你可以创建一个它们都调用的通用方法。

private boolean verifyNumericSort(...) {
    List<Integer> list = new ArrayList<Integer>();
    commonStuff1(list);
    // Do Numeric-specific stuff
    commonStuff2(list);
}

您可以将Class作为参数传递,如果这是您想要的(正如您所说,将类型作为参数传递):

public <T> void test(List<T> l, T t, Class<T> c) {
    System.out.println(c.getName());
    System.out.println(l.get(0).getClass().getName());
    System.out.println(t.getClass().getName());
}

上面的所有系统都会打印出类的名称,这样您就可以选择最适合您的了。

由于类型擦除,您不能通过使用泛型进行自省来做到这一点。但是,如果列表不是空的,则可以检查第一个元素的类型,然后调用适当的方法。

既然你有3个字段,你可以这样做…

class A
{
private Date date = null;
private Integer int = null;
private String text = null;
//add getters and setters for these fields
}

,现在把这个类Object作为参数传递给那个方法

public boolean verify(A a){
a.getDate();
a.getInt()
//etc and do your stuff
}

你需要泛型和重构:

private boolean verifyTextualSort(List<String> strings) {
    commonStuffA(strings);
    // Do textual-specific stuff
    commonStuffB(strings);
    return true; // ?
}
private boolean verifyNumericSort(List<Integer> ints) {
    commonStuffB(ints);
    // Do Numeric-specific stuff
    commonStuffB(ints);
    return true; // ?
}
private void commonStuffA(List<?> things) { // This method accept a list of anything
    // Do common stuff A with the list
}
private void commonStuffB(List<?> things) { // This method accept a list of anything
    // Do common stuff B with the list
}
private void someCallingMethod() {
    List<String> strings = new ArrayList<String>();
    verifyTextualSort(strings);
    List<Integer> ints = new ArrayList<Integer>();
    verifyTextualSort(ints);
}

我认为你可以这样做:

  public <T extends Object> boolean verify(T t)
  {
    if(!(t==null))
    {
      if(t instanceof Date)
      {
        //Do date verify routine
        return true;
      }
      else if(t instanceof String)
      {
        //Do String verify routine
        return true;
      }
      else
      {
        //Do default verify routine which could be Integer
        return true;
      }
    }
    return false;
  }

注意:

正如其他人所提到的,由于类型擦除的原因,您不能对泛型这样做(请参阅其他答案以获得类型擦除的链接)。我相信您可以使用多态性得到一个合理的解决方案(不使用instanceof)。下面是一个例子:

公共类VerifySort{public static void main(String[] args){VerifySort = new VerifySort();Date testDate = new Date();testInteger = 17;testString = "Blammy";verifySort.verify (testString);verifySort.verify (testInteger);verifySort.verify (testDate);}private boolean验证(日期参数){SimpleDateFormat = new SimpleDateFormat();system . out。print("日期参数:");System.out.println (dateFormat.format(参数);返回true;}私有布尔验证(整数参数){system . out。print("Integer parameter: ");System.out.println(参数);返回true;}私有布尔验证(字符串参数){system . out。print("字符串参数:");System.out.println(参数);返回true;}

最新更新