关于java的instanceof类型转换

关于java的instanceof类型转换

我们在上一篇文章中了解到了java比较重要的特性多态,本篇文章,承接上一篇文章中的伏笔,一起来了解一下instanceof类型转换,引用类型之间的转换😀。

一、instanceof

  • 判断一个对象是什么类型。
  • 判断两个类之间是否存在父子关系。

1、我们先创建三个类,人类,老师类,学生类,并在人类中写出一个run方法。

package oop.Demo08;
//Person
public class Person
{
    public void run()
    {
        System.out.println("run");
    }
}
package oop.Demo08;
//Student
public class Student extends Person
{
    
}
package oop.Demo08;
//Teacher
public class Teacher extends Person
{
    
}

2、我们用main方法,来使用一下instanceof。

import oop.Demo08.Student;
import oop.Demo08.Teacher;

public class Application {
    public static void main(String[] args)
    {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Object object = new Student();

        System.out.println(object instanceof Object);
        System.out.println(object instanceof Person);
        System.out.println(object instanceof Student);
        System.out.println(object instanceof Teacher);
        System.out.println(object instanceof String);//因为Object和String有联系
    }
}

我们执行以下代码,会看到以下的结果。

true
true
true
false
false

因为instanceof是判断是否存在父子类关系,我们的Teacher,和学生类是平级,所以不存在关系,String类型是引用类型,也不能作为关系的对比,就像猫不等于狗。

import oop.Demo08.Student;
import oop.Demo08.Teacher;

public class Application {
    public static void main(String[] args)
    {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Person person = new Student();

        System.out.println(person instanceof Object);
        System.out.println(person instanceof Person);
        System.out.println(person instanceof Student);
        System.out.println(person instanceof Teacher);
        //System.out.println(person instanceof String);//没有关联,编译器会报错
    }
}
true
true
true
false

我们再来看一个Student对象。

import oop.Demo08.Student;
import oop.Demo08.Teacher;

public class Application {
    public static void main(String[] args)
    {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Student student = new Student();

        System.out.println(student instanceof Object);
        System.out.println(student instanceof Person);
        System.out.println(student instanceof Student);
        //System.out.println(student instanceof Teacher);//没有关联,编译器会报错
        //System.out.println(person instanceof String);//没有关联,编译器会报错
    }
}
true
true
true
  • 公式:X instanceof Y
  • 编译是否可以通过,取决于X和Y有没有关联,有没有父子关系😎。

二、类型转换

我们在Student类中,写一个go方法,现在的Student类中有自己的go方法和父类的run方法。

package oop.Demo08;

public class Student extends Person
{
    public void go()
    {
        System.out.println("go");
    }
}

我们之前了解过,基本类型转换,由高转低,需要强转,但是低转高,不需要强转。

关于java的基本类型转换

  • 类型之间的转化:父子转换

  • 父比子高,所以不需要强制转换。

public class Application {
    public static void main(String[] args)
    {
        Person person = new Student();//Person类型
    }
}

但是我们想使用学生类中的go方法,由于学生类是低类型,Person是高类型,所以需要强制转换。

public class Application {
    public static void main(String[] args)
    {
        Person person = new Student();//Person类型
        ((Student) person).go();
    }
}

也可以用下面的方法,清晰一些。

public class Application {
    public static void main(String[] args)
    {
        Person person = new Student();
        Student student=(Student) person;
        student.go();
    }
}
  • 由低转高,不需要强转。
public class Application {
    public static void main(String[] args)
    {
        Student student = new Student();
        Person person=student;
    }
}
  • 子类转换为父类,可能丢失自己原本的方法。

三、总结

  • 父类的引用指向子类的对象。
  • 子类转换为父类,向上转型不用强制转换。
  • 想把父类转换为子类,向下转型,需要强制转换,可能会丢失方法。
  • 转换方便方法调用,不用重新new一个类,通过降级等使用类中的方法,减少重复的代码,有效提供利用率。
  • 抽象:封装,继承,多态。
  • 抽象类:比抽象还要抽象的🥲(我们放在后续的文章中详细说明)。
  • 接口:比抽象类还要抽象的🥲(我们放在后续的文章中详细说明)。
  • 编程的思想其实就是围绕抽象,所以我们要做到持续的学习,现在学习以后可能不会理解的很透彻,但是不断积累,不断实践,多多对自己的想法进行一个验证,有一天可能遇到一个问题,解决了,突然茅塞顿开。
  • 17
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值