第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

相同的變量名使用了兩次,但程序沒有拋出錯誤?

相同的變量名使用了兩次,但程序沒有拋出錯誤?

肥皂起泡泡 2022-06-23 09:15:43
在下面的鏈接列表代碼中為什么 Link在第二次調(diào)用時不newLink = new Link()給出錯誤,因為已經(jīng)定義了?thelist.insertfirst()newlink我的理解——變量的范圍newlink是方法insertfirst()。Link newLink = new Link(id, dd);創(chuàng)建鏈接時newlink,變量newlink保存創(chuàng)建的鏈接對象的內(nèi)存地址。newLink.next = first;first是一個鏈接變量,其中包含鏈接對象的內(nèi)存地址。然后,newlink.next()指向object contained at the memory address of the variable first。first = newLink;然后,變量first指向newlink. 這意味著 first 現(xiàn)在包含對象的內(nèi)存地址newlink(newlink 本身包含實際對象的地址)。當方法完成時,變量“newlink”丟失,但我們不在乎,因為我們已經(jīng)有了鏈接對象copied的內(nèi)存地址(在next字段中)。這個比喻正確嗎?class Link   {   public int iData;              // data item   public double dData;           // data item   public Link next;              // next link in list   public Link(int id, double dd) // constructor     {      iData = id;                 // initialize data      dData = dd;                 // ('next' is automatically     }                           //  set to null)////////////////////////////////////////////////////////////////class LinkList   {   private Link first;            // ref to first link on list   public LinkList()              // constructor      {      first = null;               // no links on list yet      }                              // insert at start of list   public void insertFirst(int id, double dd)      {                           // make new link      Link newLink = new Link(id, dd);//######################################## DOESN'T THROW ERROR       newLink.next = first;       // newLink --> old first      first = newLink;            // first --> newLink      }   }  // end class LinkList////////////////////////////////////////////////////////////////class LinkListApp   {   public static void main(String[] args)      {      LinkList theList = new LinkList();  // make new list      theList.insertFirst(22, 2.99);      // insert four items      theList.insertFirst(44, 4.99);      theList.insertFirst(66, 6.99);      theList.insertFirst(88, 8.99);      }  // end main()   }  // end class LinkListApp////////////////////////////////////////////////////////////////
查看完整描述

1 回答

?
呼如林

TA貢獻1798條經(jīng)驗 獲得超3個贊

你是對的,這里沒有錯誤。在方法中有一個局部變量然后多次調(diào)用該方法是非常非常常見的。是的,您可能會說第二次調(diào)用該方法時它不是同一個變量,因為每次執(zhí)行該方法時都會創(chuàng)建該變量。

您不能在同一范圍內(nèi)聲明兩個具有相同名稱的變量的原因是您不知道哪個是哪個。此問題不在您的代碼中。每次調(diào)用您的方法時,都會創(chuàng)建一個名為的變量newLink并為其分配一個值。所以當你在下面兩行中使用這個變量的名字時,它當然是新創(chuàng)建的。它不是最后一次調(diào)用該方法時創(chuàng)建的,也不是最后一次之前的時間。沒有混淆是可能的。這就是允許它的原因。

范圍是關于在程序中的什么地方,文本如果你愿意,可以使用某個名稱。在這種情況下,在它的聲明和最近的右花括號之間再往下三行。范圍與您每次被允許調(diào)用那段代碼并創(chuàng)建具有相同名稱的新變量的次數(shù)無關。換句話說,在一個范圍內(nèi),您可能只在編寫程序時鍵入具有特定名稱的變量的聲明一次,但您可以在程序運行時使用該聲明來實際創(chuàng)建變量任意多次。

是的,你的描述,你的類比是正確的。


查看完整回答
反對 回復 2022-06-23
  • 1 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號