Equivalent to C ++ variable templates in Java

I want to write a universal type observer in Java. In C ++, I can easily do this using the Variadic template from C ++ 11, for example:

class Observer<typename... T> { void update(T... args); }; 

Now, in java, the best I can do is:

 class Observer<T> { void update(T args); }; 

Now an update cannot accept several arguments of different types, for example, in C ++. Can anyone suggest a solution to this problem?

Thanks in advance.

+6
source share
2 answers

If all arguments expand / implement T, you can say:

 class Observer<T>{ void update(List<? extends T> args){} } 
+4
source

The only thing that comes to mind is the creation of a class, such as a tuple, so you do not use the variational form, but pre-set group arguments. However, this answer is highly dependent on the use case.
There is no analog analog.

0
source

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


All Articles