[英]Calling a method from a different class
我試圖使用rootNode(BSTN)的搜索遞歸地調用搜索,這給我一個錯誤,提示錯誤的類型,或者用rootNode的子樹調用getNode(錯誤,BST類型為undefined),這也給了我一個錯誤我的類型有誤
package sizebst;
public class sizeBST {
sizeBSTN rootNode;
public SizeBST(BSTN root){
rootNode = root;
}
public boolean search(int target){
//isn't complete but I want to recrusively search the tree calling methods from the other class
getNode(rootNode.LSubtree, target);
}
這是我要從中調用getNode的方法。
package sizebst;
public class SizeBSTN {
SizeBSTN LSubtree;
SizeBSTN RSubtree;
int data;
int size;
public SizeBSTN(int data){
LSubtree = null;
RSubtree = null;
this.data = data;
size = 1;
}
public static SizeBSTN getNode(SizeBSTN node, int target){
// isn't working yet but it finds a node and returns it.
}
}
由於getNode
是SizeBSTN
類的static
方法, SizeBSTN
嘗試
SizeBSTN.getNode(rootNode.LSubtree, target);
代替
getNode(rootNode.LSubtree, target);
另一種方法是使用
import static sizebst.SizeBSTN.getNode;
現在,您無需類參考即可調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.