Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无强制转换的Java继承_Java_Oop_Generics_Binary Tree - Fatal编程技术网

无强制转换的Java继承

无强制转换的Java继承,java,oop,generics,binary-tree,Java,Oop,Generics,Binary Tree,我正在为学校制作一个树/图api,从面向对象的角度来处理它。但是我正在试图找到一种方法,让继承的类将一些基类变量视为自己的类型(即,当从BSTVertex调用时,父、左、右应视为BSTVertex,但当从BinaryVertex调用时,应视为BinaryVertex),而不强制转换 我正在考虑泛型,但我不确定在这种情况下如何实现它 更新 很好,我不知道你可以在泛型中使用extend。但我得到一个错误,无法将顶点转换为T错误,如下所示: public class BinaryVertex {

我正在为学校制作一个树/图api,从面向对象的角度来处理它。但是我正在试图找到一种方法,让继承的类将一些基类变量视为自己的类型(即,当从
BSTVertex
调用时,父、左、右应视为
BSTVertex
,但当从
BinaryVertex
调用时,应视为
BinaryVertex
),而不强制转换

我正在考虑泛型,但我不确定在这种情况下如何实现它

更新 很好,我不知道你可以在泛型中使用extend。但我得到一个
错误,无法将顶点转换为T
错误,如下所示:

public class BinaryVertex {
  public BinaryVertex parent,left,right;
}

public class BSTVertex extends BinaryVertex {
  public void foo() {
    left = new BSTVertex();
    if(Math.floor(Math.random()*2) == 0) left.foo();
  }
}
公共类测试{
公共静态void main(字符串[]args){
新AVLVertex();
BSTVertex v=新的BSTVertex();
v、 foo();
}
类二元顶点{
公共T父级,左,右;
}
类BSTVertex扩展了BinaryVertex{
公共图书馆{
返回此;//此处出错
}
}
类AVLVertex扩展了顶点{
//这可能最终也是抽象的
}

foo需要返回与调用者类型相同的顶点,即,如果AVLVertex调用foo,它希望得到AVLVertex而不是BSTVertex,则可以使用如下泛型:

public class Test {
  public static void main(String[] args) {
  new AVLVertex();
  BSTVertex<BSTVertex> v = new BSTVertex<BSTVertex>();
  v.foo();
}
class BinaryVertex<T extends BinaryVertex> {
  public T parent, left, right;
}
class BSTVertex<T extends BSTVertex> extends BinaryVertex<T> {
  public T foo() {
    return this; //error here
  }
}
class AVLVertex extends BSTVertex<AVLVertex> {
  // this might probably end up being abstract too
}
public class BinaryVertex<T extends BinaryVertex<T>> {
    public T parent, left, right;
}

public class BSTVertex extends BinaryVertex<BSTVertex> {
  public void foo() {
    left = new BSTVertex();
    if(Math.floor(Math.random()*2) == 0) left.foo();
  }
}
更新

在更新的代码中(将来请改为询问新问题),您无法安全地强制转换,因为您可能会在以后编写:

public class BSTVertex extends BinaryVertex<BSTVertex> {
  private static final Random r = new Random();
  public void foo() {
    left = new BSTVertex();
    if(r.nextBoolean()) left.foo();
  }
}

这样,如果您错误地创建了
类AVLVertex extends BSTVertex{}
,它仍然会编译,但是调用
AVLVertex.foo()
时,您可能会遇到运行时
类castexception

谢谢您提供的有关扩展泛型的信息!尽管我的实现仍然存在一些问题,希望您能提供帮助(见更新后的帖子):)
class RBVertex extends BSTVertex<RBVertex>{}
class AVLVertex extends BSTVertex<RBVertex>{}
@SuppressWarnings("unchecked")
public T foo() {
    return (T) this;
}