gpt4 book ai didi

Java:如何从 String 创建类列表

转载 作者:搜寻专家 更新时间:2023-11-01 01:35:34 25 4
gpt4 key购买 nike

我想创建一个从字符串输入类名的列表。例如,

String nameOfClass = "com.my.list.class";
List list = new ArrayList<nameOfclass>();

我只知道在运行时以文本格式插入到List中的类的类型。

这可能吗?

最佳答案

没有。

泛型类型注释只发生在编译时,它们在运行时被清除。当程序执行时,ArrayList 并不知道它的泛型类型。

因此,类型注释仅对编译器有用。如果在编译时(程序设计时)不知道类型,则无法使用它们。

关于Java:如何从 String 创建类列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16161034/

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