0
泛型类型,我有以下几点:内部参数
public <T extends Node> T create(Class<T> classType, String id, Transaction t){
T obj;
try {
NodeKey nodeKey = new NodeKey(classType, id);
obj = classType.getConstructor(NodeKey.class, Transaction.class).newInstance(nodeKey, t);
add(obj, t);
return obj;
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
有你可以看到,我“构建” NodeKey nodeKey = new NodeKey(classType, id);
;我想能够超载上面的方法接受只是NodeKey
和Transaction
,但我不知道我怎么能告诉泛型T型是由像nodeKey.getUserType()
方法返回的类。
这可能吗?
你想添加一个通用参数到'NodeKey'吗?因此,'NodeKey nodeKey = new NodeKey (classType,id);'。 (和以前一样,我建议避免反射。) –
2014-09-29 12:40:16