欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java向上轉(zhuǎn)型與向下轉(zhuǎn)型超詳細(xì)圖解

 更新時(shí)間:2025年04月08日 08:36:14   作者:煎餅小狗  
我們?cè)贘ava編程中經(jīng)常碰到類型轉(zhuǎn)換,對(duì)象類型轉(zhuǎn)換主要包括向上轉(zhuǎn)型和向下轉(zhuǎn)型,這篇文章主要介紹了Java向上轉(zhuǎn)型與向下轉(zhuǎn)型的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(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搭建基本骨架

    這篇文章主要介紹了詳解mall整合SpringBoot+MyBatis搭建基本骨架,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 深入了解java中的string對(duì)象

    深入了解java中的string對(duì)象

    這篇文章主要介紹了java中的string對(duì)象,String對(duì)象是Java中使用最頻繁的對(duì)象之一,所以Java開(kāi)發(fā)者們也在不斷地對(duì)String對(duì)象的實(shí)現(xiàn)進(jìn)行優(yōu)化,以便提升String對(duì)象的性能。對(duì)此感興趣的朋友跟隨小編一起看看吧
    2019-11-11
  • java中List、Array、Map、Set等集合相互轉(zhuǎn)換

    java中List、Array、Map、Set等集合相互轉(zhuǎn)換

    這篇文章主要介紹了java中List、Array、Map、Set等集合相互轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Mybatis中的@Select、foreach用法

    Mybatis中的@Select、foreach用法

    這篇文章主要介紹了Mybatis中的@Select、foreach用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • java實(shí)現(xiàn)手機(jī)短信驗(yàn)證的基本思路

    java實(shí)現(xiàn)手機(jī)短信驗(yàn)證的基本思路

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)手機(jī)短信驗(yàn)證的基本思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • SpringBoot MainApplication類文件的位置詳解

    SpringBoot MainApplication類文件的位置詳解

    這篇文章主要介紹了SpringBoot MainApplication類文件的位置詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • java自帶排序使用

    java自帶排序使用

    這篇文章主要給大家分享了java自帶排序使用,該方法是升序排序,方法的內(nèi)部采用了快排實(shí)現(xiàn),但該方法是?穩(wěn)定的。下面一起來(lái)看看文章的詳細(xì)介紹吧
    2021-12-12
  • Java中hutool?List集合對(duì)象拷貝案例代碼

    Java中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)

    詳解設(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
  • 詳解JAVA之運(yùn)算符

    詳解JAVA之運(yùn)算符

    這篇文章主要介紹了詳解Java中運(yùn)算符以及相關(guān)的用法講解,一起跟著小編學(xué)習(xí)下吧,希望能夠給你帶來(lái)幫助
    2021-11-11

最新評(píng)論