gpt4 book ai didi

java - 使用字符串类名的通用类型调用

转载 作者:行者123 更新时间:2023-11-30 09:01:41 25 4
gpt4 key购买 nike

希望你能帮我解决这个问题:我有...

  • 名为classNameList 的类名字符串列表
  • 通用类 Geography<T>
  • 静态泛型方法<T> void read(Class<T> cl, Geography<T> geo)

我想遍历字符串类名列表并为每个类调用通用方法。

我试过但显然没有用的:

for (int i = 0; i < classNameList.length; i++) {
Class<?> myClass = Class.forName(classNameList[i].getName());
Geography<myClass.newInstance()> geo;
read(myClass, geo);
}

错误:myClass.newInstance 无法解析为类型

我的代码对于泛型函数的一次调用完美运行:

Geography<ExampleClass> ExampleGeo;
read(ExampleClass.class, ExampleGeo);

有什么想法可以做到这一点吗?

更新:

感谢您的帮助,但我仍然很难将它应用到我的实际代码中。所以这是非简单问题:

我在 shapefile-Data 中准备好一个 shapefileLoader,对于 Shapefile 的每个功能,一个类 (GuadAgent) 是用一个预定义的类 (PlantWind) 初始化的。我的输入目录中有 shapefile,其中包含它们的功能所代表的类的名称。我希望 Java 读取 shapefile 并创建相应的代理类。 (代理也被放置在上下文和地理中..)使用的类是:ShapefileLoader , Geography , 其他类可以在同一个网站上找到

这部分在主方法中:

Geography<GuadAgent> guadGeography = GeographyFactoryFinder.createGeographyFactory(null).createGeography("guadGeography", context, new GeographyParameters<GuadAgent>());
Context<GuadAgent> context = new DefaultContext<GuadAgent>();

FileFilter filter = new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".shp"); // return .shp files
}
};
String shapefileDir = System.getProperty("user.dir")+"\\input\\shp\\";
File folder = new File(shapefileDir);
File[] listOfFiles = folder.listFiles(filter);

for (File classFile : listOfFiles) {
try {
readForName(classFile,context,guadGeography);
} catch (ClassNotFoundException | MalformedURLException
| FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

读取名称的静态方法:

static <T> void readForName(File classFile, Context<GuadAgent> context,Geography<GuadAgent> guadGeography) throws ClassNotFoundException, MalformedURLException, FileNotFoundException {

String shapefileDir = System.getProperty("user.dir")+"\\input\\shp\\";
String className = classFile.getName().split("\\.(?=[^\\.]+$)")[0];
File shapefile = null;
shapefile = new File(shapefileDir+classFile.getName());

if (!shapefile.exists()) {
throw new FileNotFoundException("Could not find the given shapefile: " + shapefile.getAbsolutePath());
}

switch (className) {
case "PlantWind":
ShapefileLoader<PlantWind> PlantWindLoader = new ShapefileLoader<PlantWind>(PlantWind.class,shapefile.toURI().toURL() , guadGeography, context);
PlantWindLoader.load();
PlantWindLoader.close();
System.out.println(context.getObjects(PlantWind.class).size());
break;
// Todo Add other Agent types
default:
break;
}

我怎样才能摆脱开关?虽然他们的数量是有限的,但是有非常多不同的代理...

最佳答案

不幸的是,没有接近您意图的语法(虽然是个好主意)。

基本问题是Class.forName()返回一个未知的 Class<?> ,因此您需要在某处 进行转换。这只是你把它放在哪里的问题。

我建议这种方法(编译)捆绑起来做 read()基于类名:

static <T> void readForName(String className) throws ClassNotFoundException {
Class<T> myClass = (Class<T>) Class.forName(className);
Geography<T> geo = new Geography<T>(); // No code shown. Adjust as required
read(myClass, geo);
}

我还可以建议使用 foreach循环语法,更简洁的代码:

for (String className : classNameList) {
readForName(className.getName());
}

关于java - 使用字符串类名的通用类型调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26289147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com