简体   繁体   中英

Create instance of generic class by reflection

Is there any way to use reflection to create an instance of a generic class.

For example, how to create an instance of PriorityQueue<Integer> using reflection such as Class or Type, instead of call its constructor.

您始终可以将原始类型转换为泛型类型/从泛型类型转换。

PriorityQueue<Integer> queue = (PriorityQueue<Integer>) PriorityQueue.class.newInstance();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM