[英]How to use an interface with methods that has in input or return type the reference of the interface?
我有一个像这样的界面:
public interface IntTree {
public int getValue();
public int childrenNumber();
public int nodes ();
public int height ();
public boolean equals (IntTree t);
public void addChild (IntTree child);
public IntTree followPath(int[] path);
public void visit ();
public void printIntTree ();
}
而且我不知道如何实现MyTree
因为接口上有很多引用:例如:
public void addChild (IntTree child);
public IntTree followPath(int[] path);
public boolean equals (IntTree t);
所以我不明白如何使用IntTree
引用。 例如,我使用强制转换进行了所有方法:
public class MyTree implements IntTree {
private int data;
private LinkedList<MyTree> children; //I have to declare it IntTree or MyTree?
public MyTree(int data) {
this.data = data;
this.children = new LinkedList<>();
}
public void addChild(IntTree child){
this.children.addFirst((MyTree) child);
}
public LinkedList<MyTree> getChildren() {
return this.children;
}
}
但是我的老师说这样做是错误的,如果我不能将其转换为MyTree
对象,该如何访问我的孩子列表等? 我该如何做一个方法public IntTree followPath(int[] path);
如果MyTree object = this
并且是IntTree
的!=
? 请向我解释,我很生气:
对于其他没有任何输入且不返回接口引用的方法,我可以使用MyTree
的构造函数实现它吗?
如果允许您修改接口声明,则可以如下进行更改:
public interface IntTree<T extends IntTree<T>> {
public int getValue();
public int childrenNumber();
public int nodes ();
public int height ();
public boolean equals (T t);
public void addChild (T child);
public T followPath(int[] path);
public void visit ();
public void printIntTree ();
}
然后,您应该将您的课程声明为:
public class MyTree implements IntTree<MyTree>
现在,您的方法将如下所示:
public void addChild (MyTree child);
public MyTree followPath(int[] path);
public boolean equals (MyTree t);
这样您就可以按照自己的方式与他们合作。
私有LinkedList子级; //我必须声明它是IntTree还是MyTree?
您应该在这里使用IntTree
,然后添加实现IntTree接口的元素,以便它可以是与MyTree
不同的类对象。 您正在执行的操作类型不安全,请以以下示例为例:
public class SomeTree implements IntTree {
//some code
}
然后让SomeTree
与我们的MyTree
一起使用。
MyTree myTree = new MyTree();
SomeTree child = new SomeTree();
myTree.addChild(child); //Here we get ClassCastException
如果您需要在不更改的情况下实现IntTree
接口,则应将其保持不变而不进行转换。 如果可能,您的代码应始终是类型安全的。
equals方法是不必要的,它已经在Object中定义。 您应该覆盖它。
对于addChildren,您可以存储一个IntTree列表:
public class MyTree implements IntTree {
private int data;
private LinkedList<IntTree> children; //I have to declare it IntTree or MyTree?
public MyTree(int data) {
this.data = data;
this.children = new LinkedList<>();
}
public void addChild(IntTree child) {
children.addFirst(child);
}
public LinkedList<IntTree> getChildren() {
return children;
}
public IntTree followPath(int[] path) throws NoSuchTreeException {
for (int i = 0; i < path.length; i++) {
path[i] = path[i] - 1;
}
IntTree oggetto = this;
for (int i = 0; i < path.length; i++) {
int index = path[i];
if (index >= oggetto.getChildren().size() || oggetto.getChildren().isEmpty()) {
throw new NoSuchTreeException();
}
oggetto = oggetto.getChildren().get(i);
}
return oggetto;
}
在“ followpath”中,我更改了oggeto突击的类型。
关键是保持最通用的参考,同时避免强制转换。
定义接口时,意味着要定义将在类实现中使用的所有方法。
在界面中,您可以使用以下方法:
public interface IntTree {
public int getValue();
public int childrenNumber();
public int nodes ();
public int height ();
public boolean equals (IntTree t);
public void addChild (IntTree child);
public IntTree followPath(int[] path) throws NoSuchTreeException;
public void visit ();
public void printIntTree ();
}
这些方法必须在MyTree
类中被覆盖,例如:
public class MyTree implements IntTree {
private int data;
private LinkedList<MyTree> children; //I have to declare it IntTree or MyTree?
public MyTree(int data) {
this.data = data;
this.children = new LinkedList<>();
}
public IntTree followPath(int[] path) throws NoSuchTreeException {
for (int i = 0; i < path.length; i++) {
path[i] = path[i] - 1;
}
MyTree oggetto = this;
for (int i = 0; i < path.length; i++) {
int index = path[i];
if (index >= oggetto.getChildren().size() || oggetto.getChildren().isEmpty()) {
throw new NoSuchTreeException();
}
oggetto = oggetto.getChildren().get(i);
}
return oggetto;
}
@Override
public int getValue() {
return 0; //Implement the logic for this method
}
@Override
public int childrenNumber() {
return 0; //Implement the logic for this method
}
@Override
public int nodes() {
return 0; //Implement the logic for this method
}
@Override
public int height() {
return 0; //Implement the logic for this method
}
@Override
public boolean equals(IntTree t) {
return false; //Implement the logic for this method
}
public void addChild(IntTree child) {
this.children.addFirst((MyTree) child);
}
public LinkedList<MyTree> getChildren() {
return this.children;
}
@Override
public void visit() {
//Implement the logic for this method
}
@Override
public void printIntTree() {
//Implement the logic for this method
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.