Extract common types from extended common

I am trying to reorganize a class and a set of subclasses, where the type M really extends something, although we know that it must be a subclass of a certain type. This type is parameterized, and I would like its parameterized types to be available for subclasses that already have values ​​for M.

Is there a way to define this class without having to include redundant K and V generic types in the parameter list. I would like the compiler to be able to infer them from the fact that M maps to subclasses.

public abstract class NewParametrized<K, V, M extends SomeParametrized<K, V>> {

    public void someMethodThatTakesKAndV(K k1, V v1) { }
}

In other words, I would like the class declaration to look something like this:

 public class NewParametrized<M extends SomeParametrized<K, V>> {

And types K and V will be deduced from the definition of M.

+3
source share
2 answers

, K V "", , , - "".

, K V, M. , K V : , , - .

-, , K, V M. M K V, M .


, , , , , Java ( C, ++, , ), , , .

+6

,

public class NewParametrized<M extends SomeParametrized<K, V>> {

, K V , , .

+2

Source: https://habr.com/ru/post/1735504/


All Articles