Java instanceof关键字与类型转换

大家好,今天来分享一下instanceof关键字和类型转换

instanceof是Java的一个保留关键字,判断一个对象的是什么类型, 判断两个类是不是父子关系,左边是对象,右边是类,返回类型是Boolean类型。它的具体作用是测试左边的对象是否是右边类或者该类的子类创建的实例对象,是的话,则返回true,否则返回false。

创建Person类

package Demo03;

public class Person {
}

创建student类 ,继承于Person

package Demo03;

public class Student    extends Person{
}

创建teacher类,继承于Person类

package Demo03;

public class teacher extends Person{
}

创建Application 类,用于测试

package Demo03;

public class Application {    
    public static void main(String[] args) {     //main  方法
    
        
    }
}
package Demo03;

//Object >String
//Object  >Person >Student
         //Object  >Person >Student
     //代表着Object是Person的父类,Person
public class Application {
    public static void main(String[] args) {   //main方法
        //这个代码区是进行测试的
        //我们已经定义了相应的继承关系,

      Object object =new Student();
        System.out.println(object instanceof Student);//判断object类和student类是不是父子关系    true
        System.out.println(object instanceof Person); //判断object类和Person类是不是父子关系      true
        System.out.println(object instanceof  teacher);//判断object类和teacher类是不是父子关系      teacher   false
        System.out.println(object instanceof Object);//判断object类和Object类是不是父子关系    true      //对象指定它
        System.out.println(object instanceof  String);//判断object类和string是不是父子关系    false
    }
}

再写一个

package Demo03;

//Object >String
//Object  >Person >Student
         //Object  >Person >Student
     //代表着Object是Person的父类,Person
public class Application {
    public static void main(String[] args) {   //main方法
        //这个代码区是进行测试的
        //我们已经定义了相应的继承关系,


 


     Object object =new Student();
        System.out.println(object instanceof Student);//判断object类和student类是不是父子关系    true
        System.out.println(object instanceof Person); //判断object类和Person类是不是父子关系      true
        System.out.println(object instanceof  teacher);//判断object类和teacher类是不是父子关系      teacher   false
        System.out.println(object instanceof Object);//判断object类和Object类是不是父子关系    true
        System.out.println(object instanceof  String);//判断object类和string是不是父子关系    false

        System.out.println("============================================================");

        Person person =new Student();           //true
        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);     //这一行会报错,因为它们两者没有联系



    }

}

执行结果
在这里插入图片描述

再写一个

package Demo03;

//Object >String
//Object  >Person >Student
         //Object  >Person >Student
     //代表着Object是Person的父类,Person
public class Application {
    public static void main(String[] args) {   //main方法
        //这个代码区是进行测试的
        //我们已经定义了相应的继承关系,

      Object object =new Student();
        System.out.println(object instanceof Student);//判断object类和student类是不是父子关系    true
        System.out.println(object instanceof Person); //判断object类和Person类是不是父子关系      true
        System.out.println(object instanceof  teacher);//判断object类和teacher类是不是父子关系      teacher   false
        System.out.println(object instanceof Object);//判断object类和Object类是不是父子关系    true
        System.out.println(object instanceof  String);//判断object类和string是不是父子关系    false

        System.out.println("============================================================");

        Person person =new Student();           //true
        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);     //这一行会报错,因为它们两者没有联系



    }

}

执行结果

在这里插入图片描述

在代码的最后一行

  //    System.out.println(person instanceof  String);     //这一行会报错,因为它们两者没有联系

这一行代码的执行结果,为错误
在这里插入图片描述

Student student =new Student();

        System.out.println( student instanceof Student);   //true
        System.out.println( student instanceof Person);     //true
        //System.out.println(student instanceof  teacher);    //这一行会报错,因为两者没有关系
        System.out.println(student instanceof Object);      //true
       // System.out.println(student instanceof  String);    //这一行会报错,因为两者没有关系

显示结果

在这里插入图片描述
注意那两行会报错的代码

关于instanceof 关键字完整的代码

package Demo03;

//Object >String
//Object  >Person >Student
         //Object  >Person >Student
     //代表着Object是Person的父类,Person
public class Application {
    public static void main(String[] args) {   //main方法
        //这个代码区是进行测试的
        //我们已经定义了相应的继承关系,

      Object object =new Student();
        System.out.println(object instanceof Student);//判断object类和student类是不是父子关系    true
        System.out.println(object instanceof Person); //判断object类和Person类是不是父子关系      true
        System.out.println(object instanceof  teacher);//判断object类和teacher类是不是父子关系      teacher   false
        System.out.println(object instanceof Object);//判断object类和Object类是不是父子关系    true
        System.out.println(object instanceof  String);//判断object类和string是不是父子关系    false

        System.out.println("============================================================");

        Person person =new Student();           //true
        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);     //这一行会报错,因为它们两者没有联系

        System.out.println("=================================================");

       Student student =new Student();

        System.out.println( student instanceof Student);   //true
        System.out.println( student instanceof Person);     //true
        //System.out.println(student instanceof  teacher);    //这一行会报错,因为两者没有关系
        System.out.println(student instanceof Object);      //true
       // System.out.println(student instanceof  String);    //这一行会报错,因为两者没有关系
    }

}

整体运行结果

在这里插入图片描述

在Person类里写个方法

package Demo03;

public class Person {
    public void  run(){
        System.out.println("run");     //写个方法
        
              
    }
}

在student类当中写方法

package Demo03;

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

这里讲类型转换

package Demo03;

public class Application {
    public static void main(String[] args) {
        //类型之间的转换
        //在我们的这个程序当中是继承关系


        // 父       子
        //高                    低
        Person obj = new Student();
        //student.go       //这样无法调用student类里写的方法
        //这个时候,我们就可以使用转换
        //将Person类转换成student类型

        //我们就可以使用student类型的方法了
        //由于Person类型到Student类型的转化,是高转低,所以要强制转换
        Student student= (Student)obj;    //转换成功
        student.go();




    }
}

执行结果
在这里插入图片描述

总结:1.父类引用指向子类的对象
2.把子类转换为父类,向上转换
3.将父类转换成子类,向下转化,强制转换
4.方便方法的定义,减少重复的代码

好了,今天的instance关键字和类型转换就讲到这里了
谢谢大家

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

思诚代码块

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

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

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

打赏作者

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

抵扣说明:

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

余额充值