简体   繁体   中英

How to force generic argument superclass type when implementing Map?

I have all kinds of GameObject s. I want to make basic collection definition for them all:

//Wrong number of type arguments; required 2. > expected
public interface GameObjectMap<T> extends Map<String, T extends GameObject> {

}

The collection will allways be mapped by string (because the data are loaded from JSON ). But the second generic type argument should be any instance of GameObject . I have no idea how to write the code above correctly.

You're almost right. Simply move the extends GameObject to the first generic definition:

public interface GameObjectMap<T extends GameObject> extends Map<String, T> {

}

The restriction comes on the first declaration of T , like

public interface GameObjectMap<T extends GameObject> extends Map<String, T> {

}

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