Вопрос

I am studying for a Java course and my documentation gives this method header as generic example. Unfortunately, the description is lacking. Can someone explain what is going on with this typing?

public static <T extends Object & Comparable <? super T>> T max(T a, T b) {
...
...
...
}

Thanks!

Это было полезно?

Решение

As far as I know,

<T extends Object & Comparable <? super T>>

is equivalent with:

<T extends Comparable<? super T>>

So, this says:

T, which should be comparable to T or something that is abstracter than T.

This construction forces you to choose a T like MyClass, if MyClass is declared like one of these:

MyClass implements Comparable<MyClass>
MyClass implements Comparable<MySuperClass>
MyClass implements Comparable<Object>

But doesn't allow:

MyClass implements Comparable<String>
MyClass implements Comparable<MyChildClass>

Given that: MyChildClass extends MyClass extends MySuperClass extends Object.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top