[英]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
? 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. 在
Game
界面中? extends Result
,我知道我可以通过向Game
添加三个通用类型来做到这一点,但由于信息已经存在,因此不需要。
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. 但是,此代码在Java(Java 8)中是不合法的,因为我不能这样定义
<A, R>
。
How do I do it? 我该怎么做?
You will need to do as you initially suggest, to add "three generic types to Game
". 您将需要按照您最初的建议进行操作,向“
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. 这是在接口方法中获得
A
和R
的最佳方法。
interface Game<A extends Action, R extends Result, P extends Player<A, R>> { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.