Java:什么是向上转型与向下转型(详细图解)

目录

一、什么是向上转型

1、概念

2、代码示例

3、向上转型的优缺点

二、什么是向下转型

1、向下转型的概念

​编辑

2、代码示例

[三、向下转型的缺点及 instanceof 的使用](#三、向下转型的缺点及 instanceof 的使用)

1、向下转型的缺点

2、instanceof的使用


一、什么是向上转型

1、概念

向上转型就是创建一个子类对象 ,将其当成父类对象来使用

语法格式:父类类型 对象名 = new 子类类型()

Animal animal = new Cat ();

Animal 是父类类型,但可以引用 Cat这个子类类型,因为是从小范围到大范围的转换。

2、代码示例

java 复制代码
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方法,所以在实现时,打印的是Cat类中实现的内容。

animal3中,Dog类 没有重写display方法,所以打印的还是父类中的内容。

由此我们可以得出:向上转型实现时

先看子类有没有

若是子类找不到

再看父类有没有

二者都无 则报错!

3、向上转型的优缺点

优点:让代码实现更简单灵活

缺点:不能调用到子类特有的方法

例如:

java 复制代码
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(); //会报错
    }
}

所以,向上转型无法调用子类特有的方法!

二、什么是向下转型

1、向下转型的概念

将一个子类对象向上转型之后可以当成父类对象使用,若需要调用子类特有的方法,则需要将父类对象再还原为子类对象。这就称作向下转型。

2、代码示例

java 复制代码
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方法,在向下转型之前如果调用eat方法会报错
        //向下转型为子类Dog类后,就可以调用子类中特有的方法,而不会报错
        animal = (Dog)animal;
        ((Dog) animal).eat();
    }
}

运行结果:

三、向下转型的缺点及 instanceof 的使用

1、向下转型的缺点

缺点:向下转型使用的比较少,而且不安全。如果转换失败,运行时就会抛异常。

2、instanceof的使用

Java中为了提高向下转型的安全性,引入了instanceof 。如果表达式为 true,则可以安全转换。

使用实例:

java 复制代码
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();
        
        //判断instanceof 是否为 true
        if(animal instanceof Dog) {
            //向下转型
            animal = (Dog)animal;
            ((Dog) animal).eat();
        } else {
            System.out.println("Animal无法向下转型为Dog");
        }
    }
}

以上就是**Java:什么是向上转型与向下转型(详细图解)**的全部内容了,希望能对您有所帮助!

您的点赞与收藏就是对我最大的支持!

相关推荐
贫民窟的勇敢爷们2 小时前
SpringBoot整合AOP切面编程实战,实现日志统一记录+接口权限校验
java·spring boot·spring
jerryinwuhan2 小时前
基于各城市站点流量的复合功能比较
开发语言·php
浅念-3 小时前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
Kiling_07043 小时前
Java集合进阶:Set与Collections详解
算法·哈希算法
AC赳赳老秦3 小时前
供应链专员提效:OpenClaw自动跟踪物流信息、更新库存数据,异常自动提醒
java·大数据·服务器·数据库·人工智能·自动化·openclaw
迈巴赫车主3 小时前
Java基础:list、set、map一遍过
java·开发语言
智者知已应修善业3 小时前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
·醉挽清风·3 小时前
学习笔记—MySQL—库表操作
笔记·学习·mysql
灵犀学长4 小时前
基于 Spring ThreadPoolTaskScheduler + CronTrigger 实现的动态定时任务调度系统
java·数据库·spring
洛水水4 小时前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展