我有一个泛型类 Storage,还有另一个泛型类 StoragePlus。我收到下面粗体注释的行的有界错误不匹配错误。
这是我第一堂课的代码。
public class Storage<T extends Comparable<? super T>> {
//private T t;
private T[] isafe;
public int max;
private int nextIndex;
public Storage(int maxNum, T[] seedArr){
if(maxNum < 0){
throw new IllegalArgumentException();
}
else{
isafe = Arrays.copyOf(seedArr, maxNum);
nextIndex = 0;
max = maxNum;
}
}
这是第二类的代码。
public class StoragePlus <T> extends Storage<T>{ **//getting an error here**
public StoragePlus(int maxNum, T[] arr){
super(maxNum,(Comparable[]) arr);
}
@Override
public boolean equals(Object obj){
if(obj == null|| obj.getClass() != this.getClass()){
return false;
}
else{
if(obj == this){
return true;
}
else{
@SuppressWarnings("unchecked")
Storage<?> temp = (Storage<T>) obj; **// also getting an error here**
}
}
}
}
我想你想要
Storage<T extends Comparable<T>>
StoragePlus<T extends Comparable<T>> extends Storage<T>
在平等的情况下,您只能将Object
投给Storage<?>
.否则,您需要@SuppressWarnings
.
因为你没有限制StoragePlus
的T
。