Good day.
As a person accustomed to C ++ and templates, I constantly have some misunderstandings with generics.
So, suppose we have printers that can print some objects:
public interface Printer<X> { public void print(X obj); }
There is a list on which printer and what to print:
class PrintInfo<X> { public Printer<X> printer; public X obj; } Queue<PrintInfo<?>> q;
Now you want to start typing:
PrintInfo<?> pi = q.poll(); pi.printer.print(pi.obj); // FAIL
The problem is that for some reason Java does not see the emphasis, that this is a correct challenge, although the connection of types seems to be obvious. Please explain why this is happening, and what are the ways to get around it?
PS I went around by adding the print
method to PrintInfo
, which actually prints its object on its printer and calls it.