Java向上轉(zhuǎn)型與向下轉(zhuǎn)型超詳細(xì)圖解
一、什么是向上轉(zhuǎn)型
1、概念
向上轉(zhuǎn)型就是創(chuàng)建一個(gè)子類對象,將其當(dāng)成父類對象來使用。
語法格式:父類類型 對象名 = 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類 沒有重寫 display方法,所以打印的還是父類中的內(nèi)容。
由此我們可以得出:向上轉(zhuǎn)型實(shí)現(xiàn)時(shí)
先看子類有沒有
若是子類找不到
再看父類有沒有
二者都無則報(bào)錯(cuò)!
3、向上轉(zhuǎn)型的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):讓代碼實(shí)現(xià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)型無法調(diào)用子類特有的方法!

二、什么是向下轉(zhuǎn)型
1、向下轉(zhuǎn)型的概念
將一個(gè)子類對象向上轉(zhuǎn)型之后可以當(dāng)成父類對象使用,若需要調(diào)用子類特有的方法,則需要將父類對象再還原為子類對象。這就稱作向下轉(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類中原本沒有 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無法向下轉(zhuǎn)型為Dog");
}
}
}
總結(jié)
到此這篇關(guān)于Java向上轉(zhuǎn)型與向下轉(zhuǎn)型的文章就介紹到這了,更多相關(guān)Java向上轉(zhuǎn)型與向下轉(zhuǎn)型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
@RequestBody,@RequestParam和@Param的區(qū)別說明
這篇文章主要介紹了@RequestBody,@RequestParam和@Param的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Maven項(xiàng)目繼承實(shí)現(xiàn)過程圖解
這篇文章主要介紹了Maven項(xiàng)目繼承實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
使用RestTemplate調(diào)用https接口跳過證書驗(yàn)證
這篇文章主要介紹了使用RestTemplate調(diào)用https接口跳過證書驗(yàn)證,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
關(guān)于ReadWriteLock讀寫鎖的使用及說明
這篇文章主要介紹了關(guān)于ReadWriteLock讀寫鎖的使用及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
java判斷String類型是否能轉(zhuǎn)換為int的方法
今天小編就為大家分享一篇java判斷String類型是否能轉(zhuǎn)換為int的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

