generics - How to interpret "public <T> T readObjectData(... Class<T> type)" in Java? -
i have java code.
public <t> t readobjectdata(bytebuffer buffer, class<t> type) { ... t retval = (t) summaries; return retval;
how interpret code? why need public <t> t
instead of public t
?
how give parameter 2nd argument (class<t> type
)?
this declares readobjectdata
method generic, 1 type parameter, t
.
public <t> ...
then return type t
.
... t readobjectdata(...
without initial <t>
, generic type declaration, symbol t
undefined.
in parameter list, class<t> type
1 of parameters. because return type , parameter both reference t
, ensures if pass in class<string>
, return string
. if pass in class<double>
, return double
.
to pass in parameter, pass in class
object, e.g. string.class
.
Comments
Post a Comment