If I specific a method which return a generic class,how can I do than I can specific the type of generic class dynamicly ?
for example
try {
Class c =Class.forName(keytype);
Class d= Class.forName(valuetype);
KafkaConsumer consumerconsumer = new KafkaConsumer(PropertiesUtil.getPropsObj(configPath));
return consumer ;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
But the code above is not OK. How can I do than I can achieve that?
解决方案
You can have your key and value class each implement a known interface.
Then you can assign or cast it.
KafkaConsumer consumerconsumer = new KafkaConsumer<>(PropertiesUtil.getPropsObj(configPath));
or
KafkaConsumer consumerconsumer1 = (KafkaConsumer) new KafkaConsumer(PropertiesUtil.getPropsObj(configPath));