通用可比内部 class
Generic Comparable inner class
您好,我正在尝试构建一个简单的 SelfSortingList。这不是为了任何现实世界的使用,所以我这样做是为了学习。
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;
}
}
}
在 addValue 方法中重新分配父对象 'root' 值以指向新项目时,我收到此错误消息:
错误:(41, 27) java:不兼容的类型:com.company.SelfSortingList.Node 无法转换为 com.company.SelfSortingList.Node
所以SelfSortingList.Node不能转换成自己的类型?
我不知道该如何看待这条错误消息。将 SelfSortingList 和 Item 的 class 声明更改为没有 'extends Comparable R' 并没有改变这件事。
我自己找到了答案。
而不是说:
root=(Item<R>)node;
我不得不这样投射它:
root=(SelfSortingList.Item)node;
您收到错误消息是因为您的私有 class Item
中的类型参数 "R" 隐藏了 class SelfSortingList
.
重命名内部class的类型参数(例如"S"),你会看到你尝试将类型Item<S>
(节点)分配给类型Item<R>
(根).
您好,我正在尝试构建一个简单的 SelfSortingList。这不是为了任何现实世界的使用,所以我这样做是为了学习。
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;
}
}
}
在 addValue 方法中重新分配父对象 'root' 值以指向新项目时,我收到此错误消息: 错误:(41, 27) java:不兼容的类型:com.company.SelfSortingList.Node 无法转换为 com.company.SelfSortingList.Node
所以SelfSortingList.Node不能转换成自己的类型?
我不知道该如何看待这条错误消息。将 SelfSortingList 和 Item 的 class 声明更改为没有 'extends Comparable R' 并没有改变这件事。
我自己找到了答案。 而不是说:
root=(Item<R>)node;
我不得不这样投射它:
root=(SelfSortingList.Item)node;
您收到错误消息是因为您的私有 class Item
中的类型参数 "R" 隐藏了 class SelfSortingList
.
重命名内部class的类型参数(例如"S"),你会看到你尝试将类型Item<S>
(节点)分配给类型Item<R>
(根).