Day013 instanceof和类型转换
作者:互联网
instanceof
instanceof是Java的一个二元操作符,和==, >, <是同一类东东。由于它是由字母组成的,所以也是Java的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据。
假设有三个类,Person、Teacher、Student,Student和Teacher继承人
public class Person {
}
public class Student extends Person {
}
public class Teacher extends Person{
}
public static void main(String[] args) {
//Object>String
//Object>Person>Teacher
//Object>Person>Student
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
}
输出结果
true
true
true
false
false
换成Person引用类型看一下
public static void main(String[] args) {
//Object>String
//Object>Person>Teacher
//Object>Person>Student
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("=======================");
Person person=new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
//System.out.println(person instanceof String);//编译报错
}
输出结果
true
true
true
false
false
=======================
true
true
true
false
把Person再换成Student
public static void main(String[] args) {
//Object>String
//Object>Person>Teacher
//Object>Person>Student
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("=======================");
Person person=new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
//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 Object);//true
//System.out.println(student instanceof Teacher);//编译报错
//System.out.println(student instanceof String);//编译报错
}
输出结果:
true
true
true
false
false
=======================
true
true
true
false
=======================
true
true
true
结论:
x instanceof y 是否能编译通过取决于x的引用类型是否与y是父子关系
类型转换
回顾基本类型转换
低------------------------------------------------------>高
byte,short,char->int->long->float->double
低转高自动转换,高转低需要强制转换
引用类型转换
父类(高) 子类(低)
因此父类转子类是高转低,需要强制转换;子类转为父类型是低转高,可以自动转换。
现在在person类里面加run方法,student里加go方法
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person {
public void go(){
System.out.println("go");
}
}
public static void main(String[] args) {
Person obj=new Student();//低转高自动转换
//obj.go();编译报错
}
可以发现obj.go()报错了,因为自动把Student类型转换为了Person类型,go是Sudent类的方法,要想使用Student的方法,需要把Person类型强制转换为Student类型
public static void main(String[] args) {
Person obj=new Student();//低转高自动转换
//obj.go();编译报错
Student student=(Student) obj;
student.go();
}
输出结果
go
再来看一个例子
//自动类型转换
Student student = new Student();
student.go();
Person person=student;
//person.go();//编译报错
输出结果
go
发现子类Student转为父类Person之后,就不能调go()方法了
结论 :父类(高)转为子类(低)需要强制转换;子类(低)转父类(高)可以自动转换,子类转为父类可能会丢失一些子类的方法。
总结
- 子类转换为父类,向上转型,自动转换,可能会丢失子类的方法
- 父类转换为子类,向下转型,强制转换
- 类型转换的好处:方便方法的调用,减少重复的代码。
标签:instanceof,类型转换,System,println,Student,Day013,true,out 来源: https://www.cnblogs.com/dwystudy/p/14766240.html