If you want to make a class use generics, add <T>
after the class name
and before the constructor (if there is one). You can then use T
as a placeholder
for the generic type, such as in constructor parameters.
Here, we have a Transport
that can only transport objects of type T
.
Since critterCarrier
is passed a Frog
, it will be a Transport<Frog>
and
can only transport frogs.
You can learn more about this in:
Tags: