从 Java 中给定的 Class 对象创建对象
Creating object from a given Class object in Java
关于在给定 Class 对象时创建对象的快速问题。或者,也许我需要以不同的方式解决这个问题。首先,根据我的计划,我正在编写一个方法,该方法将获取一个 File 对象数组,并将每个对象读入一个 Set,然后将每个 set 附加到一个列表并返回该列表。以下是我的资料:
private static List<Set<String>> loadFiles(File[] files, Class whatType, Charset charSet){
List<Set<String>> setList = new ArrayList<Set<String>>(files.length);
try {
for(File f : files){
BufferedInputStream bs = new BufferedInputStream(new FileInputStream(f));
InputStreamReader r = new InputStreamReader(bs, charSet);
BufferedReader br = new BufferedReader(r);
Set<String> set = new HashSet<>(); //This is the problem line
String line = null;
while( (line = br.readLine()) != null){
set.add(line.trim());
}
br.close();
setList.add(set);
}
return setList;
} catch (FileNotFoundException e) {
//Just return the empty setlist
return setList;
} catch (IOException e) {
//return a new empty list
return new ArrayList<Set<String>>();
}
}
但我想要的是允许该方法的用户指定要实例化的 Set 的类型(当然只要它包含字符串)。这就是 'whatType' 参数的用途。
我的所有研究都引导我如何实例化给定 class 名称的对象,但这并不是我真正想要的。
如果你假设 class 有一个无参数的可访问构造函数,你基本上是一个 newInstance()
调用:
Set<String> set = (Set<String) whatType.newInstance();
请注意,如果您将 whatType
定义为 Class<? extends Set>
而不是原始的 Class
,您也可以摆脱这种丑陋的转换。
如果你会使用Java8,你可以轻松解决这个问题。方法声明如下:
private static List<Set<String>> loadFiles(File[] files, Supplier<Set> setSupplier, Charset charSet)
将您的问题行更改为:
Set<String> set = setSupplier.get();
然后,在每次调用此方法时,可以使用方法引用轻松提供 setSupplier 参数:HashSet::new、TreeSet::new...
使用 Class.newInstance() 方法怎么样?我为您编写了一个简单的示例:
public <T extends Set> void myMethod(Class<T> type) {
T object;
try {
object = type.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public void caller() {
myMethod(HashSet.class);
}
这是您要找的吗?
关于在给定 Class 对象时创建对象的快速问题。或者,也许我需要以不同的方式解决这个问题。首先,根据我的计划,我正在编写一个方法,该方法将获取一个 File 对象数组,并将每个对象读入一个 Set,然后将每个 set 附加到一个列表并返回该列表。以下是我的资料:
private static List<Set<String>> loadFiles(File[] files, Class whatType, Charset charSet){
List<Set<String>> setList = new ArrayList<Set<String>>(files.length);
try {
for(File f : files){
BufferedInputStream bs = new BufferedInputStream(new FileInputStream(f));
InputStreamReader r = new InputStreamReader(bs, charSet);
BufferedReader br = new BufferedReader(r);
Set<String> set = new HashSet<>(); //This is the problem line
String line = null;
while( (line = br.readLine()) != null){
set.add(line.trim());
}
br.close();
setList.add(set);
}
return setList;
} catch (FileNotFoundException e) {
//Just return the empty setlist
return setList;
} catch (IOException e) {
//return a new empty list
return new ArrayList<Set<String>>();
}
}
但我想要的是允许该方法的用户指定要实例化的 Set 的类型(当然只要它包含字符串)。这就是 'whatType' 参数的用途。
我的所有研究都引导我如何实例化给定 class 名称的对象,但这并不是我真正想要的。
如果你假设 class 有一个无参数的可访问构造函数,你基本上是一个 newInstance()
调用:
Set<String> set = (Set<String) whatType.newInstance();
请注意,如果您将 whatType
定义为 Class<? extends Set>
而不是原始的 Class
,您也可以摆脱这种丑陋的转换。
如果你会使用Java8,你可以轻松解决这个问题。方法声明如下:
private static List<Set<String>> loadFiles(File[] files, Supplier<Set> setSupplier, Charset charSet)
将您的问题行更改为:
Set<String> set = setSupplier.get();
然后,在每次调用此方法时,可以使用方法引用轻松提供 setSupplier 参数:HashSet::new、TreeSet::new...
使用 Class.newInstance() 方法怎么样?我为您编写了一个简单的示例:
public <T extends Set> void myMethod(Class<T> type) {
T object;
try {
object = type.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public void caller() {
myMethod(HashSet.class);
}
这是您要找的吗?