简体   繁体   中英

Java generics why doesn't this work?

为什么不能使用以下泛型创建地图?

Map<Class<K extends Item>, K> classMap;

Because Map is already generified - your job when creating a reference is to fill in the type parameter. Unless this is inside a method parameterized with K , the compiler will have no idea what K should be replaced with (and if it were inside a parameterized method, you couldn't have K extends Item in the body - K either already extends Item , or it doesn't).

New type parameters can go in the signatures of classes and methods with the implicit promise that they will be filled in later. They cannot go inside declarations.

Here you are making an instance of Map , not defining it. K needs to be a specific class.

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