generics - Code explanation in Java -
this morning came across code, , have absolutely no idea means. can explain me these <t>
represent? example:
public class myclass<t> ... bits of code private something<t> so; private otherthing<t> to; private class<t> c;
thank you
you have bumped "generics". explained nicely in guide.
in short, allow specify type storage-class, such list
or set
contains. if write set<string>
, have stated set must contain string
s, , compilation error if try put else in there:
set<string> stringset = new hashset<string>(); stringset.add("hello"); //ok. stringset.add(3); ^^^^^^^^^^^ //does not compile
furthermore, useful example of generics can allow more closely specify abstract class:
public abstract class abstclass<t extends variable> {
in way, extending classes not have extend variable
, need extend class extends variable
.
accordingly, method handles abstclass
can defined this:
public void dothing(abstclass<?> abstextension) {
where ?
wildcard means "all classes extend abstclass
variable
".
Comments
Post a Comment