java的instanceof和强制类型转换

instance of和类型转换

1.instance of是什么?

instance of 这个关键字可以判断两个类之间是否存在父子关系。

2.怎么使用instanceof?

  • 咱们开始之前要先建立四个程序,这个四个程序可以弄明白他的原理。

    • person类(父类)
    public class person {
    
    }
    
    • student类(子类)
    public class student extends person {
    
    }
    
    • teacher类(子类)
    public class teacher extends person{
    
    }
    
    • 主程序

      public class application {
        public static void main(String[] args) {
            Object object = new student();     //父类的引用指向了子类 
            System.out.println(object instanceof student);   //true
            System.out.println(object instanceof person);    //ture
            System.out.println(object instanceof teacher);   //false
            System.out.println(object instanceof Object);    //true
            System.out.println(object instanceof String);    //false
        }
      }
      ==================================================
      最终的输出结果:
      true
      true
      false
      true
      false
      

    咱们在之前讲的多态,不难搞懂Object object = new student();是什么意思,这个表示是父类Object 的引用指向了子类student,这样去new的话,必须是继承关系才行。也正这个new,决定了下面不同的instance of的输出结果。

  1. 首先咱们看一下new的这一句话
Object object = new student();     //父类的引用指向了子类

Object类作为所有类的父类,同时person类是student类的父类,因此这个new可以得出来三层关系:

Object>person>student

  1. 这只是从这个三个类之间进行比较,咱们上面说到过,Object类作为所有类的父类,那么Object类也是teacher类的父类,那么teacher类在Object object = new student();这条语句下,和他们三个有什么关系呢?咱们看一下上面代码及其输出结果。
Object object = new student();
System.out.println(object instanceof teacher);   //false

咱们最最最开始,提到过instance of 不是可以判断两个类之间是否存在父子关系吗,那么在这怎么输出false呢,因为这跟new也是有关系的,咱们是用Object object = new student();来new的student类,即Object类(父类)的引用指向了student类(子类),那么这个instance of也会根据Object到student之间所存在的所有关系进行判断,即Object>person>student,显然teacher是不存在于这三个关系之间,因此是false。

  1. 目前主程序里面,涉及到student类,teacher类,person类,Object类,以及String类,那么这些类之间存在什么样的关系呢。
Object>String    //String类是属于Object类下面的
Object>person>student
Object>person>teacher

以上就是他们之间的关系。

  1. 还有一种情况,同级之间进行比较,他是会报错的,运行都运行不了。例如person和String就是相同级别,student和teacher就是相同级别,咱们看一下代码,要想出现同级之间比较的话,咱们需要重新new一个对象。
  • 主程序
public class application {
    public static void main(String[] args) {
        person Person = new student();       //person类的引用指向了student类
        System.out.println(Person instanceof student);    //true
        System.out.println(Person instanceof person);     //true
        System.out.println(Person instanceof teacher);    //false
        System.out.println(Person instanceof Object);     //true
        System.out.println(Person instanceof String);     //报错
    }
}

咱们去掉最后一个输出。

public class application {
    public static void main(String[] args) {
        person Person = new student();
        System.out.println(Person instanceof student);    //true
        System.out.println(Person instanceof person);     //true
        System.out.println(Person instanceof teacher);    //false
        System.out.println(Person instanceof Object);     //true
    }
}
==================================================
    最终输出结果:
true
true
false
true

咱们在刚才说过,同级之间是无法比较的,他会报错的

Object>String    //String类是属于Object类下面的
Object>person>student
Object>person>teacher

在这咱们可以看出person和String是同级,所以该行代码会报错。

再按照我刚才所说比较方法,去看看上面其他代码的输出结果。这个是person类的引用指向了student类,所以满足

person>student之间的比较,输出结果即为true。

5.咱们再看一下另一种new的方法。

  • 主程序
public class application {
    public static void main(String[] args) {
        student Student = new student();
        System.out.println(Student instanceof student);       //true
        System.out.println(Student instanceof person);        //ture
        System.out.println(Student instanceof teacher);       //报错
        System.out.println(Student instanceof Object);        //true
        System.out.println(Student instanceof String);        //报错
    }
}

这个时候发现new的方法不一样了 student Student = new student();因为student和teacher同级,所以报错,因为student和String没有任何关系所以报错,但是student和person还有Object比较却是true,因为咱们刚才说过他们的关系

Object>String    //String类是属于Object类下面的
Object>person>student
Object>person>teacher

student可能属于person之下,也可能在Object之下,所以是true。咱们看他能不能通过,则是看比较的两个有没有父子关系,同级(没有父子关系)也会出现编译错误。

3.类型转换

  1. 咱们之前学过的类型转换,有个是基本类型转换,例如,64位 32位 16位,高转低需要强制转换,低转高,不需要强制转换。
  2. 咱们在这所学的父类和子类,父类就是高的,子类就是低的。
public class application {
    public static void main(String[] args) {
        //高                   低
        Object object = new student();//该行代码是低转高,不需要强制转换。由student类变成Object类。
    }
}
  1. 咱们看看类型转换怎么用
  • person类(父类)
public class person {
    void run(){
        System.out.println("run");
    }
}
  • student类(子类)
public class student extends person {
void go(){
    System.out.println("go");
    }
}     
  • 主程序
public class application {
    public static void main(String[] args) {
        //高                   低
        person obj = new student();//该行代码是低转高,不需要强制转换。
       obj.go();  //报错,不能运行程序
    }
}

上面的方法调用是报错的,因为person类是student类的父类,只有student类里面有go,而person类里面没有go,所以没有办法调用。

我们若想要使用go方法,需要将person类的obj对象转换成转换成student类型的,就可以使用student类型的方法。

  1. 转换格式:(类型)对象名; 然后按住alt+enter即可,就会出现下面
student student1 = (student) obj;
  1. 咱们继续看主程序的代码
public class application {
    public static void main(String[] args) {
        //高                   低
        person obj = new student();//该行代码是低转高,不需要强制转换。
        student student1 = (student) obj;  //该转换是由person类转换到student类,由高转低。
        student1.go(); //这是没有问题的
    }
}
===========================================
    输出结果:
    go
  1. 还有另一种强制转换就是((student)obj).go(); 代码如下
public class application {
    public static void main(String[] args) {
        person obj = new student();
        ((student)obj).go();
    }
}
=======================================
    最终输出结果
    go

7.当他自然转换的时候,也就是从低转高,他会出现丢失方法的情况。例如

public class application {
    public static void main(String[] args) {
        student student = new student();
        person person0 = student ;    //低转高,不需要强制转换   这个也是父类的引用指向子类的对象
        ((student)person0).go();   //采用了强制转换,也就是person0对象里面没有go方法.student对象是有go的,所以丢失 
                                   //方法了
    }
}
===============================================
    最终输出结果
    go

所以,子类转换为父类,可能丢失自己的本来的一些方法!

  1. 把子类转换为父类,向上转型。

​ 把父类转换为子类,向下转型,强制转换。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GaoJa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值