简体   繁体   English

将getter与Java通用方法参数一起使用

[英]using getter with java generic method argument

I have a method that takes a generic parameter type. 我有一个采用通用参数类型的方法。 The scenario I have is this method will be called with different parameter types. 我在这种情况下会用不同的参数类型调用此方法。

class something{
    public void someMethod(){
        List<A> listA = ....; //Class A have a field String Id;
        List<B> listB = ....; //Class B haave a field String Id;

        testMethod(listA);
        testMethod(listB);
    }

    private <T> void testMethod( List<T> list ){
            for( T event : list ){
                //TODO (something like): event.getId();
            }
        }
}

In the above code all the parameters will be be a List<someObjectType> . 在上面的代码中,所有参数都是List<someObjectType> All the object types have a common field and need to use the getter to fetch its value. 所有对象类型都有一个公共字段,需要使用getter来获取其值。 Now since the method definition is generic, how do I achieve this? 现在,由于方法定义是通用的,我该如何实现?

Have A and B implement a common interface that has a method getID : AB实现具有方法getID的公共接口:

interface SomeInterface {
    String getID();
}

then you could have: 那么您可以拥有:

private <T extends SomeInterface> void testMethod(List<T> list) {
    for (T event : list) {
        // now you can use `event.getID()` here
    }
}

This cannot be done. 无法做到这一点。 You can't handle two different lists with incompatible interfaces the same way in your method, unless you do something with instanceof , ie 您不能在方法中以相同的方式处理具有不兼容接口的两个不同列表,除非您对instanceof进行了某些操作,即

public void testMethod(List<? extends Object> list) {
  if(list.get(0) == null) return;
  if(list.get(0) instanceof A) {
    // Do the A stuff
  } else {
    // Do the B stuff
  }
}

There is no point in creating such a generic method without bounded type. 创建没有边界类型的泛型方法毫无意义。 Since T isn't bounded to any type, you can't use specific interface on the objects inside the list. 由于T不受任何类型的限制,因此您不能在列表内的对象上使用特定的接口。 So if you want testMethod to get list of objects of any type, you should use List<?> instead. 因此,如果希望testMethod获取任何类型的对象列表,则应改用List<?>

Define your method like this with T extending your common class/interface BaseType : 用T扩展您的公共类/接口BaseType定义您的方法:

private <T extends BaseType> void testMethod( List<T> list ){
   for( T event : list ){
       //TODO (something like): event.getId();
   }
}

Example: 例:

public void someMethod() {
    List<Integer> listA = Arrays.asList( new Integer[] {1, 4, 9} );
    List<Double> listB = Arrays.asList( new Double[] {1.5, 4.2, 9.3} );;
    testMethod(listA);
    testMethod(listB);
}

private <T extends Number> void testMethod( List<T> list ){
    for( T event : list ) {
        // Note intValue() method is being called here which is provided
        // in the base class Number that is being extended by T
        System.out.println(event.intValue());
    }
}   

As other answers said, you need to bound the type parameter by some interface. 就像其他答案所说的那样,您需要通过一些接口来绑定type参数。 But for what you're using it for, you don't actually need to have a T : 但是对于您使用它的目的,实际上并不需要T

private void testMethod(List<? extends SomeInterface> list) {
    for (SomeInterface event : list) {
        // now you can use `event.getID()` here
    }
}

I don't know if i really understand what you want. 我不知道我是否真的了解你想要什么。 But if you know, you will store for example Strings into your List and want to use the toUpperCase() method, how about just casting it? 但是,如果您知道,您将例如将字符串存储到列表中,并希望使用toUpperCase()方法,那么如何将其转换呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM