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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

就類變量而言,上拋和下鑄有什么區(qū)別?

就類變量而言,上拋和下鑄有什么區(qū)別?

qq_遁去的一_1 2019-07-10 10:00:54
就類變量而言,上拋和下鑄有什么區(qū)別?對(duì)于類變量,上拋和下拋有什么區(qū)別?例如,在下面的程序類中,Properties只包含一個(gè)方法,但是Dog類包含兩個(gè)方法,然后我們?nèi)绾螌og變量轉(zhuǎn)換為動(dòng)物變量。如果轉(zhuǎn)換完成了,那么我們?nèi)绾斡脛?dòng)物的變量調(diào)用狗的另一個(gè)方法。class Animal {      public void callme()     {         System.out.println("In callme of Animal");     }}class Dog extends Animal {      public void callme()     {         System.out.println("In callme of Dog");     }     public void callme2()     {         System.out.println("In callme2 of Dog");     }}public class UseAnimlas {     public static void main (String [] args)      {         Dog d = new Dog();               Animal a = (Animal)d;         d.callme();         a.callme();         ((Dog) a).callme2();     }}
查看完整描述

3 回答

?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

向下鑄造和向上鑄造如下:

上澆鑄當(dāng)我們想要將子類轉(zhuǎn)換為超類時(shí),我們使用更新(或加寬)。這是自動(dòng)發(fā)生的,不需要顯式地做任何事情。

下播:當(dāng)我們想要將一個(gè)超類轉(zhuǎn)換為子類時(shí),我們使用Downcast(或縮窄),并且在Java中不能直接進(jìn)行下播,我們必須明確地這樣做。

Dog d = new Dog();Animal a = (Animal) d; //Explicitly you have done upcasting. Actually no need, we can directly type cast like Animal a = d; 
compiler now treat Dog as Animal but still it is Dog even after upcastingd.callme();a.callme(); 
// It calls Dog's method even though we use Animal reference.((Dog) a).callme2(); 
// Downcasting: Compiler does know Animal it is, In order to use Dog methods, we have to do typecast explicitly.
// Internally if it is not a Dog object it throws ClassCastException

分享


查看完整回答
反對(duì) 回復(fù) 2019-07-10
  • 3 回答
  • 0 關(guān)注
  • 440 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)