سؤال

public abstract class SomeClass<Sometype extends OtherClass> extends Something   implements   Comparator<??>{

//code

How can I replace ?? in Comparator such that it is any class that extends SomeClass?

هل كانت مفيدة؟

المحلول

It's not clear if you want to carry the type-bounds or simply allow any subclass of SomeClass.

By definition, the compare method you implement will allow comparison with any subtype (because any subtype can be cast to the more general parent type when passed to the method).

To allow any parameterized type instead of the restrictive Sometype, you can write:

public abstract class SomeClass<Sometype extends OtherClass> extends Something
   implements Comparator<SomeClass<?>>

It's also legal to write this with explicit type bounds:

public abstract class SomeClass<Sometype extends OtherClass> extends Something
   implements Comparator<SomeClass<Sometype>>

نصائح أخرى

you cant use something like ? extends SomeClass , but you can use just Comparator<SomeClass> and cast to what you need

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top