简体   繁体   中英

How to infer type argument of ? extends Class in interface declaration

I am developing some interfaces for a game and I came to the following interface definitions so far:

  • public interface Player<A extends Action, R extends Result> { }

  • public interface Game<P extends Player<? extends Action, ? extends Result>> { }

However now I would like to access the ? extends Result ? extends Result in the Game interface, I know I could do it by adding three generic types to Game , but that should not be needed as the information is already there.

I would want something like the following:

public interface <A, R> Game<P extends Player<A extends Action, R extends Result>> { }

This code however is not legal in Java (Java 8) as I cannot define <A, R> like this.

How do I do it?

You will need to do as you initially suggest, to add "three generic types to Game ".

As long as you create the proper bounds when the generic type parameters are declared, you can use them to define another type parameter.

That is the best way to get A and R available in your interface's methods.

interface Game<A extends Action, R extends Result, P extends Player<A, R>> { }

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