Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/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_Generic Programming - Fatal编程技术网

Java 泛型可比内类

Java 泛型可比内类,java,generic-programming,Java,Generic Programming,嗨,我正在尝试建立一个简单的自分类列表。这不是任何真实世界的使用,所以我这样做是为了学习 public class SelfSortingList<R extends Comparable<R>> { private Item<R> root; public SelfSortingList(){ root=null; } public void add(R value){ if(root==null) root=new It

嗨,我正在尝试建立一个简单的自分类列表。这不是任何真实世界的使用,所以我这样做是为了学习

public class SelfSortingList<R extends Comparable<R>> {

private Item<R> root;

public SelfSortingList(){
    root=null;
}
public void add(R value){
    if(root==null)
        root=new Item<>(value, null);
    else
        root.addValue(value);
}

@Override
public String toString() {
    return root.toString();
}

/*Inner class*/
private class Item<R extends Comparable<R>> {

    private Item<R> parent, child;
    private R value;

    private Item(R value, Item<R> parent) {
        this.value = value;
        this.parent = parent;
    }

    protected void addValue(R other) {
        if (other.compareTo(this.value) > 0) {
            System.out.println("child add");
            if(child!=null) {
                child.addValue(other);
            }else{
                child = new Item<>(other, this);
            }
        } else {
            Item<R> node = new Item<R>(other,parent);
            if(this!=root) {
                parent.child = node;
            }else{
                root= (Item<R>) node; //This is where i get trouble
            }
            node.child=this;
        }
    }

    @Override
    public String toString() {
        String str = value.toString() + ", ";
        if(child!=null)
            str+=child.toString();
            return str;
        }
    }
}
公共类自排序列表{
私有项根;
公共自排序列表(){
root=null;
}
公共无效添加(R值){
if(root==null)
根=新项(值,空);
其他的
root.addValue(value);
}
@凌驾
公共字符串toString(){
返回root.toString();
}
/*内部阶级*/
私人类项目{
私有项父项、子项;
私人R值;
私有项(R值,项父项){
这个值=值;
this.parent=parent;
}
受保护的无效附加值(R其他){
如果(其他.compareTo(此.value)>0){
System.out.println(“子添加”);
if(child!=null){
附加值(其他);
}否则{
子项=新项(其他,本项);
}
}否则{
项目节点=新项目(其他,父项);
if(this!=根){
parent.child=节点;
}否则{
root=(Item)node;//这就是我遇到麻烦的地方
}
node.child=this;
}
}
@凌驾
公共字符串toString(){
字符串str=value.toString()+“,”;
if(child!=null)
str+=child.toString();
返回str;
}
}
}
在addValue方法中,当重新分配父对象“根”值以指向新项时,我收到以下错误消息: 错误:(41,27)java:不兼容的类型:com.company.SelfSortingList.Node无法转换为com.company.SelfSortingList.Node

所以SelfSortingList.Node无法转换为自己的类型


我不知道该如何看待这个错误消息。将SelfSortingList和Item的类声明更改为不带“extends Comparable R”不会改变问题。

我自己找到了答案。 而不是说:

root=(Item<R>)node;

您会收到错误消息,因为您的私有类
项中的类型参数“R”隐藏了类
自排序列表的类型参数“R”

重命名内部类的类型参数(例如“S”),您将看到您试图将类型
(节点)分配给类型
(根)

root=(SelfSortingList.Item)node;