gpt4 book ai didi

java - 创建通过反射获得的对象集合

转载 作者:行者123 更新时间:2023-12-01 22:29:25 25 4
gpt4 key购买 nike

假设我有一些 Object oo instanceof XXX == true 。我现在想(在运行时)创建 XXX 类型的 ArrayList,如 ArrayList<XXX>() 。有什么办法可以做到这一点吗?我目前只创建 ArrayList<Object>但这还不够。

最佳答案

When runtime inspecting a parameterizable type itself, like java.util.List, there is no way of knowing what type is has been parameterized to. This makes sense since the type can be parameterized to all kinds of types in the same application. But, when you inspect the method or field that declares the use of a parameterized type, you can see at runtime what type the paramerizable type was parameterized to.

http://tutorials.jenkov.com/java-reflection/generics.html

<小时/>

不幸的是,您无法在运行时获取参数化类型。泛型的目的是提供编译类型的安全性。希望上面的链接有帮助 =)

关于java - 创建通过反射获得的对象集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28126426/

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