大家好,今天来分享一下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关键字和类型转换就讲到这里了
谢谢大家