[英]Java Generics: Generics not within bounds at initialization
I am working with generics in Java™, and have found that I have a slight problem. 我正在使用Java™中的泛型,但发现我有一个小问题。 I know Java™ uses type erasure ; 我知道Java™使用类型擦除 ; however, I need to get the class of the generic at run-time. 但是,我需要在运行时获取泛型的类。 Therefore, I use a simple runaround to find the class of the generic, described here . 因此,我用一个简单搪塞找到类通用的,描述在这里 。 The issue I am having is when I compile, javac
spits out this: 我遇到的问题是在编译时, javac
吐出了以下内容:
C:\blah\Board.java:65: error: type argument T#1 is not within bounds of type-variable T#2
this.boardManager = new NumberBoardManager<T>(c);
^
where T#1,T#2 are type-variables:
T#1 extends Comparable<? super T#1> declared in class Board
T#2 extends Number,Comparable<? super T#2> declared in class NumberBoardManager
C:\blah\Board.java:65: error: constructor NumberBoardManager in class NumberBoardManager<T#2> cannot be applied to given types;
this.boardManager = new NumberBoardManager<T>(c);
^
required: Class<T#1>
found: Class<CAP#1>
reason: actual argument Class<CAP#1> cannot be converted to Class<T#1> by method invocation conversion
where T#1,T#2 are type-variables:
T#1 extends Comparable<? super T#1> declared in class Board
T#2 extends Number,Comparable<? super T#2> declared in class NumberBoardManager
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
The first error makes sense, as the class I am in, Board
has a generic that only extends Comparable<? super T>
第一个错误是有道理的,就我所处的类别而言, Board
具有仅扩展Comparable<? super T>
的泛型Comparable<? super T>
Comparable<? super T>
, while this specific constructor uses the fact that I want (but not necessarily have) a generic that also extends Number
. Comparable<? super T>
,而这个特定的构造函数使用的事实是我想要(但不一定要有)泛型也扩展Number
。 I want to fix this issue by having the generic I use to create the boardManager
to extend both Number
and Comparable<? super T>
我想通过让我用来创建boardManager
的泛型来扩展Number
和Comparable<? super T>
来解决此问题Comparable<? super T>
Comparable<? super T>
, and to error out if the generic doesn't extend Number
. Comparable<? super T>
,如果泛型不扩展Number
,则会出错。 But I don't know of any syntax which would help me at this point. 但是我不知道有什么语法可以帮助我。
The second error is caused because I have already initialized boardManager
with the generic T
. 导致第二个错误是因为我已经使用通用T
初始化了boardManager
。 But if we know T
implements Comparable<? super T>
但是如果我们知道T
实现Comparable<? super T>
Comparable<? super T>
, why does this error get thrown? Comparable<? super T>
,为什么会引发此错误? I hope with the solving of the first issue, that the second issue is taken care of as well. 我希望在解决第一个问题的同时,也要解决第二个问题。 Here is some code to provide context, and I am willing to provide more if you are still confused. 这是一些提供上下文的代码,如果您仍然感到困惑,我愿意提供更多。
public class Board<T extends Comparable<? super T>> extends Object implements Serializable
{
private BoardManager<T> boardManager;
public Board(int size)
{
Class<?> c = ((Class<?>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
if(c.isAssignableFrom(Number.class))
{
this.boardManager = new NumberBoardManager<T>(c); //this is the line
}
}
}
public class NumberBoardManager<T extends Number & Comparable<? super T>> extends Object implements BoardManager<T>, Serializable
{
private Class<T> c;
public NumberBoardManager(Class<T> c)
{
this.c = c;
}
}
Any and all help would be appreciated! 任何和所有帮助将不胜感激!
I want to fix this issue by having the generic I use to create the boardManager to extend both Number and Comparable, and to error out if the generic doesn't extend Number. 我想通过让我用来创建boardManager的泛型来扩展Number和Comparable来解决此问题,并在泛型不扩展Number时出错。
You can't do that. 你不能那样做。
You can't have methods on a class that are only allowed with certain type parameterizations; 您不能在类上只有某些类型的参数化才允许使用方法。 the class must be able to fully function with any allowed type parameter. 该类必须能够使用任何允许的type参数完全发挥作用。
You need to constrain the entire Board
class to have T
be Number
. 您需要限制整个Board
类使其T
为Number
。
The other error is because Class<?>
isn't compatible with Class<T>
. 另一个错误是因为Class<?>
与Class<T>
不兼容。 <?>
means that the type parameter might be any possible type; <?>
表示type参数可以是任何可能的类型; it might be something that violates T
's constraint. 它可能违反了T
的约束。
You need to cast to Class<T>
. 您需要强制转换为Class<T>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.