[英]How to extend Generic abstract Class with <E extends Comparable<E>>
I'm trying to make an abstract class AbstractBinSearchTree
like this: 我正在尝试使一个抽象类AbstractBinSearchTree
像这样:
public abstract class AbstractBinSearchTree<E extends Comparable<E>>
{
public abstract void add(E element);
public abstract void add(Collection<E> elemente);
public List<E> retrieveInOrder(Pos<E> p) {
...
}
My question is now how to extend from this class. 我的问题现在是如何从此类扩展。 I want to make an ArrayBinTreeSearch
which I can instantiate like this: 我想制作一个ArrayBinTreeSearch
,可以像这样实例化:
AbstractBinSearchTree<Elem> tree = new ArrayBinSearchTree<Elem>();
I tried this: 我尝试了这个:
public class ArrayBinSearchTree<E extends Comparable<? super E>> extends AbstractBinSearchTree<Comparable<E>>{
Comparable<E>[] _tree;
public ArrayBinSearchTree(int size)
{
_tree = new Comparable[size];
}
@SuppressWarnings("unchecked")
@Override
public void add(Comparable<?> element) {
...
}
But I doesn't really feel like this is the correct way to use it. 但是我并不真的觉得这是使用它的正确方法。 Also I cannot instantiate like I mentioned above. 同样,我无法像上面提到的那样实例化。
I believe you need to replace the following line: 我相信您需要替换以下行:
public class ArrayBinSearchTree<E extends Comparable<? super E>> extends AbstractBinSearchTree<Comparable<E>>{
With: 附:
public class ArrayBinSearchTree<E extends Comparable<? super E>> extends AbstractBinSearchTree<E>{
Otherwise you'll restrict the parameters of any methods in AbstractBinSearchTree
. 否则,您将限制AbstractBinSearchTree
中任何方法的参数。 In addition, expand the generic type of AbstractBinSearchTree
as follows: 此外,按如下所示扩展AbstractBinSearchTree
的通用类型:
public abstract class AbstractBinSearchTree<E extends Comparable<? super E>>
Also, your ArrayBinSearchTree constructor has an int
parameter: 另外,您的ArrayBinSearchTree构造函数具有一个int
参数:
AbstractBinSearchTree<Elem> tree = new ArrayBinSearchTree<Elem>(5);
And can be replaced by diamond syntax: 并且可以用菱形语法代替:
AbstractBinSearchTree<Elem> tree = new ArrayBinSearchTree<>(5);
Assuming you're using Java 7 or higher 假设您使用的是Java 7或更高版本
与它的界面相当,然后必须将“扩展”更改为“实现”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.