简体   繁体   中英

Generic type that accepts types that are implementing Comparable by inheriting it

How to make DataStructure to accept type B without using raw types?

public class Main {
    public static void main(String[] args) {
        DataStructure<A> bo = new DataStructure<>();

        //This doesn't compile because of T extends Comparable<T> 
        DataStructure<B> bst = new DataStructure<>();
    }
}

class DataStructure<T extends Comparable<T>> {
}

abstract class A implements Comparable<A> {
}

abstract class B extends A /*implements Comparable<B> - illegal*/ {
}

I can change DataStructure signature to: class DataStructure<T extends Comparable> but then I get warning about raw type. I can't do class DataStructure<T extends Comparable<? extends T>> class DataStructure<T extends Comparable<? extends T>> because when T == B the ? extends T ? extends T is obviously false.

DataStructure<T extends Comparable<? super T>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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