简体   繁体   English

如何推断的类型参数? 在接口声明中扩展Class

[英]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. 这是在接口方法中获得AR的最佳方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方法参数extends class implements interface - Method argument extends class implements interface 如何验证泛型在Java中扩展了另一个类/接口? - How to verify a generic type extends another class/interface in Java? 如何准备类型的参数:列表 <Entry<? extends Class<?> ,?&gt;&gt; - How to I prepare an argument of type: List<Entry<? extends Class<?>, ?>> Java 8如何推断lambdas参数类型 - How Java 8 is able to infer lambdas argument type 无法推断类型参数<r>地图(函数<!--? super T,? extends R--> ) 在某些特定情况下</r> - Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>) in some specific situation 需要参数扩展特定类并实现特定接口 - Requiring an argument extends a particular class AND implements a particular interface java接口输入参数从基类扩展而来 - java interface input argument extends from a base class Java:如何创建一个泛型类,它需要一个扩展类并实现接口的数据类型? - Java: how to create a generic class that requires a data type which extends a class and implements an interface? Java如何返回扩展接口的泛型类型 - Java how to return a generic type that extends an interface 复杂泛型类型声明的解释。 什么类 <? extends Class<? extends Actor> []&gt;实际意味着什么? - Explanation for convoluted generic-type declaration. What does Class<? extends Class<? extends Actor>[]> actually mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM