通用二叉树Java

时间:2017-07-14 07:42:37

标签: java generics interface

我的任务是在java中用类Node和Tree和NodeActionInterface编写一个通用的二叉树

public interface NodeActionInterface {
public void action(); }

这是我的节点:

public class Node <T> implements NodeActionInterface {

<T> data;
Node leftTree;
Node rightTree;

public <T> Node(<T> data){
   this.data = data;
}
 @Override
    public void action() {

    }

但是有一些错误,例如&#34;标识符预期&#34;和其他一些人。 有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

嗯,有很多语法错误:

public class Node <T> implements NodeActionInterface
{
    // <T> data;
    T data;
//  ^ T is the data type...

    Node<T> leftTree;
    Node<T> rightTree;
//      ^  not really an errror, but you should use the generic here

    //public <T> Node(<T> data)
    public Node(T data)
//        ^ declared another T hiding the "outer" T
//              ^ again: data type is T
    {
        this.data = data;
    }
}

答案 1 :(得分:-1)

实际上,您提供的信息并不多。可以尝试这将得到一个线索。这是给定代码的语法更正。

public class Node <T> implements NodeActionInterface  {

T data;
Node leftTree;
Node rightTree;

public T Node(T data){        
    this.data = data;
    return data;
}