简体   繁体   中英

Java Queue of Interface type

I'm trying to make a queue of an interfaced type Player because I don't know what type of player will be in the queue, ie human, AI etc so I have an interface for what different players can do, ie makemove etc.

Queue<Player> players = new Queue<Player>();

However, queue cannot be instantiated because Player is an interface. How do I create a queue of an interfaced type?

You can have a variable of type Queue<Player> , but Queue itself is just an interface. You need to instantiate a concrete implementation of Queue , such as LinkedList .

eg

Queue<Player> players = new LinkedList<Player>();

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