Java:什么是向上转型与向下转型(详细图解)
创始人
2025-01-15 01:03:18
0

目录

一、什么是向上转型

1、概念

2、代码示例

3、向上转型的优缺点

二、什么是向下转型

1、向下转型的概念

​编辑

2、代码示例

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

1、向下转型的缺点

2、instanceof的使用


一、什么是向上转型

1、概念

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

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

                     Animal   animal  =  new Cat ();

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

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

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

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

                                先看子类有没有

                                若是子类找不到

                                再看父类有没有

                                二者都无则报错!

3、向上转型的优缺点

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

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

例如:

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、代码示例

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,则可以安全转换。

使用实例:

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:什么是向上转型与向下转型(详细图解)的全部内容了,希望能对您有所帮助!

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

相关内容

热门资讯

八分钟了解!newpoker怎... 八分钟了解!newpoker怎么安装脚本,哈糖大菠萝能开挂吗,指南书教程(有挂分析)1、哈糖大菠萝能...
方案辅助!微信小程序微乐破解器... 方案辅助!微信小程序微乐破解器2024!解谜真的是有辅助教程(有挂细节)1、进入到微信小程序微乐破解...
第9分钟了解!德普之星有辅助软... 第9分钟了解!德普之星有辅助软件吗,德州局透视脚本,步骤教程(有挂神器)运德普之星有辅助软件吗辅助工...
窍要辅助!洞庭茶苑app辅助!... 窍要辅助!洞庭茶苑app辅助!关于存在有辅助神器(有挂辅助)1.洞庭茶苑app辅助 选牌创建新账号,...
七分钟了解!wepoker怎么... 七分钟了解!wepoker怎么开辅助,wepoker透视脚本免费app,绝活儿教程(有挂细节)1、w...
窍要辅助!嘟咪互动有挂吗!开挂... 窍要辅助!嘟咪互动有挂吗!开挂是有辅助软件(有挂总结)窍要辅助!嘟咪互动有挂吗!开挂是有辅助软件(有...
1分钟了解!wepoker辅助... 1分钟了解!wepoker辅助器最新版本更新内容,德普之星私人局辅助免费,办法教程(有挂辅助)wep...
大纲辅助!心悦海南苹果版辅助器... 大纲辅助!心悦海南苹果版辅助器!关于是有辅助工具(有挂攻略)1、玩家可以在心悦海南苹果版辅助器线上大...
指南辅助!小程序广东雀神智能插... 指南辅助!小程序广东雀神智能插件安装下载!解谜真的是有辅助技巧(新版有挂)运小程序广东雀神智能插件安...
第九分钟了解!wepoker作... 第九分钟了解!wepoker作弊辅助,wpk辅助购买,步骤教程(新版有挂)1、完成wepoker作弊...