Sunday 15 July 2012

java - Using generic for custom type -



java - Using generic for custom type -

i working methods write every type in custom way. these signatures:

public void write(boolean value); public void write(byte value); ... public void write(integer value); public void write(long value);

as signatures same changing type, have been thinking on using generics , method receives unknown type , uses right method. similar this:

public <t> void dowrite(t data) { write(data); }

in case compiler gives me error:

the method write(boolean) in type author not applicable arguments (t).

any hint on how ?

as rule of thumb, should consider using generics when code gets lot of type querying (casts, instanceof). purpose eliminate casting. case seems not of kind. on contrary introduce type querying refactoring code way ask, don't think apropriate

java generics

No comments:

Post a Comment