博客
关于我
8.JAVA-向上转型、向下转型
阅读量:438 次
发布时间:2019-03-06

本文共 2919 字,大约阅读时间需要 9 分钟。

父子对象之间的转换分为了向上转型向下转型,它们区别如下:

  • 向上转型 : 通过子类对象(小范围)实例化父类对象(大范围),这种属于自动转换
  • 向下转型 : 通过父类对象(大范围)实例化子类对象(小范围),这种属于强制转换

1. 向上转型

示例1-向上转型

class A {         public void print() {                  System.out.println("A:print");         }}class B extends A {         public void print() {                          System.out.println("B:print");         }}public class Test{         public static void main(String args[])         {                  A a = new B();          //通过子类去实例化父类                  a.print();         }}

运行打印:

 

如上图所示,可以看到打印的是class B的print,这是因为我们通过子类B去实例化的,所以父类A的print方法已经被子类B的print方法覆盖了.从而打印classB的print.

类似于C++的virtual虚函数。

这样做的意义在于:

  • 当我们需要多个同父的对象调用某个方法时,通过向上转换后,则可以确定参数的统一.方便程序设计(参考下面示例)

 

示例2-向上转型的作用分析

class A {         public void print() {                  System.out.println("A:print");         }}class B extends A {         public void print() {                          System.out.println("B:print");         }}class C extends B {         public void print() {                          System.out.println("C:print");         }}public class Test{         public static void func(A a)         {                  a.print();         }         public static void main(String args[])         {                  func(new B());  //等价于 A a =new B();                  func(new C());  //等价于 C c =new C();         }}

运行打印:

 

PS:向上转型时,父类只能调用父类方法或者子类覆写后的方法,而子类中的单独方法则是无法调用的.

 

2. 向下转型

在java中,向下转型则是为了,通过父类强制转换为子类,从而来调用子类独有的方法(向下转型,在工程中很少用到).

为了保证向下转型的顺利完成,在java中提供了一个关键字:instanceof,通过instanceof可以判断某对象是否是某类的实例,如果是则返回true,否则为false,instanceof使用如下:

A a = new B();                 //向上转型 (B类是A的子类)a instanceof A;                //返回true.a instanceof B;                //返回truea instanceof C;                //返回false

接下来,我们便来分析向下转型的意义.

 

示例-向下转型的作用分析

class A {         public void print() {                  System.out.println("A:print");         }}class B extends A {         public void print() {                          System.out.println("B:print");         }         public void funcB(){                  System.out.println("funcB");         }}class C extends A {         public void print() {                          System.out.println("C:print");         }         public void funcC(){                  System.out.println("funcC");         }}public class Test{         public static void func(A a)         {                  a.print();                  if(a instanceof B)                  {                          B b = (B)a;   //向下转型,通过父类实例化子类                          b.funcB();    //调用B类独有的方法                  }                  else if(a instanceof C)                  {                          C c = (C)a;  //向下转型,通过父类实例化子类                          c.funcC();   //调用C类独有的方法                  }         }         public static void main(String args[])         {                  func(new A());                     func(new B());                  func(new C());         }}

运行打印:

 

从上面打印可以看到,我们成功通过向下转型来调用B类和C类独有的方法.

 

下章学习: 

 

转载地址:http://xviyz.baihongyu.com/

你可能感兴趣的文章
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>