Java向上轉(zhuǎn)型與向下轉(zhuǎn)型超詳細(xì)圖解
一、什么是向上轉(zhuǎn)型
1、概念
向上轉(zhuǎn)型就是創(chuàng)建一個(gè)子類對(duì)象,將其當(dāng)成父類對(duì)象來(lái)使用。
語(yǔ)法格式:父類類型 對(duì)象名 = new 子類類型()
Animal animal = new Cat ();
Animal 是父類類型,但可以引用 Cat這個(gè)子類類型,因?yàn)槭菑男》秶酱蠓秶霓D(zhuǎn)換。
2、代碼示例
class Aminal { public void display() { System.out.println("Animal"); } } class Cat extends Aminal { public void display() { System.out.println("Cat"); } } class Dog extends Aminal { } public class Main{ public static void main(String[] args) { Aminal aminal1 = new Aminal(); Aminal aminal2 = new Cat(); Aminal aminal3 = new Dog(); aminal1.display(); aminal2.display(); aminal3.display(); } }
animal2中,Cat類 重寫了 display方法,所以在實(shí)現(xiàn)時(shí),打印的是Cat類中實(shí)現(xiàn)的內(nèi)容。
animal3中,Dog類 沒(méi)有重寫 display方法,所以打印的還是父類中的內(nèi)容。
由此我們可以得出:向上轉(zhuǎn)型實(shí)現(xiàn)時(shí)
先看子類有沒(méi)有
若是子類找不到
再看父類有沒(méi)有
二者都無(wú)則報(bào)錯(cuò)!
3、向上轉(zhuǎn)型的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):讓代碼實(shí)現(xiàn)更簡(jiǎn)單靈活
缺點(diǎn):不能調(diào)用到子類特有的方法
例如:
class Animal { public void display() { System.out.println("Animal"); } } class Dog extends Animal { public void display() { System.out.println("dog"); } public void eat() { System.out.println("吃骨頭"); } } public class Main { public static void main(String[] args) { Animal animal = new Dog(); animal.display(); animal.eat(); //會(huì)報(bào)錯(cuò) } }
所以,向上轉(zhuǎn)型無(wú)法調(diào)用子類特有的方法!
二、什么是向下轉(zhuǎn)型
1、向下轉(zhuǎn)型的概念
將一個(gè)子類對(duì)象向上轉(zhuǎn)型之后可以當(dāng)成父類對(duì)象使用,若需要調(diào)用子類特有的方法,則需要將父類對(duì)象再還原為子類對(duì)象。這就稱作向下轉(zhuǎn)型。
2、代碼示例
class Animal { public void display() { System.out.println("Animal"); } } class Dog extends Animal { public void display() { System.out.println("dog"); } public void eat() { System.out.println("吃骨頭"); } } public class Main{ public static void main(String[] args) { //向上轉(zhuǎn)型 Animal animal = new Dog(); animal.display(); //向下轉(zhuǎn)型 //Animal類中原本沒(méi)有 eat方法,在向下轉(zhuǎn)型之前如果調(diào)用eat方法會(huì)報(bào)錯(cuò) //向下轉(zhuǎn)型為子類Dog類后,就可以調(diào)用子類中特有的方法,而不會(huì)報(bào)錯(cuò) animal = (Dog)animal; ((Dog) animal).eat(); } }
運(yùn)行結(jié)果:
三、向下轉(zhuǎn)型的缺點(diǎn)及 instanceof 的使用
1、向下轉(zhuǎn)型的缺點(diǎn)
缺點(diǎn):向下轉(zhuǎn)型使用的比較少,而且不安全。如果轉(zhuǎn)換失敗,運(yùn)行時(shí)就會(huì)拋異常。
2、instanceof的使用
Java中為了提高向下轉(zhuǎn)型的安全性,引入了instanceof。如果表達(dá)式為 true,則可以安全轉(zhuǎn)換。
使用實(shí)例:
class Animal { public void display() { System.out.println("Animal"); } } class Dog extends Animal { public void display() { System.out.println("dog"); } public void eat() { System.out.println("吃骨頭"); } } public class Main { public static void main(String[] args) { //向上轉(zhuǎn)型 Animal animal = new Dog(); //判斷instanceof 是否為 true if(animal instanceof Dog) { //向下轉(zhuǎn)型 animal = (Dog)animal; ((Dog) animal).eat(); } else { System.out.println("Animal無(wú)法向下轉(zhuǎn)型為Dog"); } } }
總結(jié)
到此這篇關(guān)于Java向上轉(zhuǎn)型與向下轉(zhuǎn)型的文章就介紹到這了,更多相關(guān)Java向上轉(zhuǎn)型與向下轉(zhuǎn)型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解mall整合SpringBoot+MyBatis搭建基本骨架
這篇文章主要介紹了詳解mall整合SpringBoot+MyBatis搭建基本骨架,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08java中List、Array、Map、Set等集合相互轉(zhuǎn)換
這篇文章主要介紹了java中List、Array、Map、Set等集合相互轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2017-05-05java實(shí)現(xiàn)手機(jī)短信驗(yàn)證的基本思路
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)手機(jī)短信驗(yàn)證的基本思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11SpringBoot MainApplication類文件的位置詳解
這篇文章主要介紹了SpringBoot MainApplication類文件的位置詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java中hutool?List集合對(duì)象拷貝案例代碼
這篇文章主要給大家介紹了關(guān)于Java中hutool?List集合對(duì)象拷貝的相關(guān)資料,介紹了如何將兩個(gè)不同對(duì)象(Point和CustomData)的特定字段拷貝到一個(gè)中間對(duì)象(IotDataCache)中,并討論了一些在實(shí)現(xiàn)過(guò)程中遇到的問(wèn)題和解決方法,需要的朋友可以參考下2024-11-11詳解設(shè)計(jì)模式中的proxy代理模式及在Java程序中的實(shí)現(xiàn)
代理模式主要分為靜態(tài)代理和動(dòng)態(tài)代理,使客戶端方面的使用者通過(guò)設(shè)置的代理來(lái)操作對(duì)象,下面來(lái)詳解設(shè)計(jì)模式中的proxy代理模式及在Java程序中的實(shí)現(xiàn)2016-05-05