亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未找到符號:但是例如檢查?

未找到符號:但是例如檢查?

飲歌長嘯 2023-08-16 16:16:59
class Node {   String guide;   // guide points to max key in subtree rooted at node}class InternalNode extends Node {   Node child0, child1, child2;   // child0 and child1 are always non-null   // child2 is null if node has only 2 children}class LeafNode extends Node {   // guide points to the key   int value;}所以我有3個不同的課程。節點。以及擴展 Node 的內部節點和葉節點。內部節點具有字段 child0、child1 和 child 2。但是,當我運行此代碼時,即使我在第一個 if 語句中檢查了對象的實例。當 p 為節點類型時嘗試訪問 child0、child1 或 child2 時,我仍然收到“符號未找到錯誤”?我的邏輯有什么問題嗎?if ( p instanceof LeafNode || p instanceof Node) {   System.out.println(p.guide);} else {   if (p.child2 == null) {       printAll(p.child0);       printAll(p.child1);   } else {       printAll(p.child0);       printAll(p.child1);       printAll(p.child2);    }}
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

問題是 p 是節點類型而不是InternalNode。


您需要做的是將 p 轉換為 InternalNode,然后再訪問僅存在于 InternalNode 中的變量。


if (p instanceof InternalNode) {

    InternalNode pInternal = (InternalNode) p;

    // access node0 and so on here.

}

您的代碼應如下所示:

if ( p instanceof LeafNode || p instanceof Node){

    System.out.println(p.guide);

} else if (p instanceof InternalNode) {

    InternalNode internalNode = (InternalNode) p;

     if (internalNode.child2 == null){

         printAll(internalNode.child0);

         printAll(internalNode.child1);

     } else {

         printAll(internalNode.child0);

         printAll(internalNode.child1);

         printAll(internalNode.child2);

     }

}


查看完整回答
反對 回復 2023-08-16
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號