Дженерики Java Wildcarding с несколькими классами

С дополнительной информацией из комментариев становится ясно, что вы находитесь в Windows, а pg_dump нет в вашем PATH.

Либо используйте полный абсолютный путь для pg_dump, либо добавьте каталог в переменную среды PATH.

367
задан Jeff Axelrod 5 May 2012 в 01:38
поделиться

2 ответа

Actually, you can do what you want. If you want to provide multiple interfaces or a class plus interfaces, you have to have your wildcard look something like this:

<T extends ClassA & InterfaceB>

See the Generics Tutorial at sun.com, specifically the Bounded Type Parameters section, at the bottom of the page. You can actually list more than one interface if you wish, using & InterfaceName for each one that you need.

This can get arbitrarily complicated. To demonstrate, see the JavaDoc declaration of Collections#max, which (wrapped onto two lines) is:

public static <T extends Object & Comparable<? super T>> T
                                           max(Collection<? extends T> coll)

why so complicated? As said in the Java Generics FAQ: To preserve binary compatibility.

It looks like this doesn't work for variable declaration, but it does work when putting a generic boundary on a class. Thus, to do what you want, you may have to jump through a few hoops. But you can do it. You can do something like this, putting a generic boundary on your class and then:

class classB { }
interface interfaceC { }

public class MyClass<T extends classB & interfaceC> {
    Class<T> variable;
}

to get variable that has the restriction that you want. For more information and examples, check out page 3 of Generics in Java 5.0. Note, in , the class name must come first, and interfaces follow. And of course you can only list a single class.

583
ответ дан Eddie 23 November 2019 в 00:08
поделиться

Параметр типа может иметь несколько границ. Например,

public static <T extends Number & Comparable<T>> T maximum(T x, T y, T z)

T является параметром типа, передаваемым в универсальный класс Box, и должен быть подтипом класса Number и должен иметь значение Comparable interface. В случае, если класс передается как связанный, он должен быть передан прежде, чем интерфейс, иначе произойдет ошибка времени компиляции.

public static <T extends Number & Comparable<T>> T maximum(T x, T y, T z) {
    T max = x;      
    if(y.compareTo(max) > 0) {
        max = y;   
    }

    if(z.compareTo(max) > 0) {
        max = z;                    
    }
    return max;      
}

Подробнее здесь

0
ответ дан 16 September 2019 в 22:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: