java - incompatible types: T#1 cannot be converted to T#2 -
i going create 1 generic class, first want requirement. have different classes e.g. a,b etc. going create instance of class based on json object. json object read file. file may contain equivalent json object.based on create instance of class using gson. face 1 error i.e. incompatible types: t#1 cannot converted t#2
this code sample
public class jsonloader<t> { private final gson gson = new gson(); private final t content; public <t> jsonloader(class<t> clazz, string filepath) throws illegalfileexception { if (filepath.isempty() || filepath == null) { throw new illegalfileexception("illegalfileexception: source file must required."); } try (reader reader = new filereader(filepath)) { t content= gson.fromjson(reader, clazz); this.content = content; } catch (ioexception e) { throw new illegalfileexception(e.getmessage(),e); } } public <t> t getobject() { return this.content; } }
please me.
when declare type parameter t
on class, entire class body has access type parameter, don't need redeclare it. when public <t> jsonloader
, public <t> t getobject
declaring new type parameters same name shadow type parameter on class.
this similar how can declare variable shadows field:
class example { int foo; // parameter foo shadows field foo example(int foo) {} }
if remove type parameter declarations on constructor , method should work.
Comments
Post a Comment