instanceof和类型转换
instanceof判断类型
通过instanceof判断对象是什么类型
两类之间存在父子关系则instanceof会ok,否则不行
public class Person {
}
public class Student extends Person{
}
public class Teacher extends Person{
}
import OOP.duotai.Person;
import OOP.duotai.Student;
import OOP.duotai.Teacher;
public class Application {
public static void main(String[] args) {
Object object = new Student();
System.out.println(object instanceof Student);
System.out.println(object instanceof Person);
System.out.println(object instanceof Object);
System.out.println(object instanceof Teacher);
System.out.println(object instanceof String);
}
}
输出:
true
true
true
false
false
即:此时object即是Student类,又是Person类,又是Object类。
Object > Person > Student(判断/定义类型时可向上转型)
Object > Person > Teacher
Object > String
类型关键看new什么,倘若是声明父类但new子类来生成的对象,在判断类型时可自动向上转型,即它既是子类类型,又是父类类型;但若new父类生成的对象,则其在判断类型时,不是子类类型。
且有上下继承关系时才会互相转/判断为true:
public class Application {
public static void main(String[] args) {
Object object = new Student();
Person person = new Student();
System.out.println(person instanceof Student);
System.out.println(person instanceof Person);
System.out.println(person instanceof Object);
System.out.println(person instanceof Teacher);
System.out.println(person instanceof String);//此行会编译报错,因为本身Student与Person两个类与String并没有直接关系,因此不可以直接用instanceof来连接
}
}
输出:
true
true
true
false
System.out.println(person instanceof Teacher);
//此行会False:因为new的Student本身与Teacher是并列(兄弟关系)并非上下继承关系,因此不可转型,而Person本身是Teacher的父类,不可向下转型True子类的类型
public class Application {
public static void main(String[] args) {
Object object = new Student();
Person person = new Student();
Student student = new Student();
System.out.println(student instanceof Student);
System.out.println(student instanceof Person);
System.out.println(student instanceof Object);
System.out.println(student instanceof Teacher);
//这行也会编译报错,因为Student与Teacher并没有直接关系,不可以直接instanceof连接
}
}
输出:
true
true
true
两者之间若没有直接继承关系,则不可比较;
(子类是父类的一种,而父类不一定是子类的一种,如猫科动物(父类)不一定是老虎(子类),而老虎(子类)一定是猫科动物(父类))
若通过new Person来判断Teacher,仍会False:因为不可向下转型;即判断类型时只可True父类(子类是父类的一种,而父类不一定是子类的一种)
Person person = new Person();
System.out.println(person instanceof Teacher);
输出:False
注:声明的类型是用来开内存的模板,实际上生成什么类型的对象要看new,能装入模板的属性与方法就装,否则就用模板自带的
类型转换
之前的类型转换:64位,32位等,高转低需要强制转换,低转高可自动
现在是父子类型的转换,父为高,子为低:
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
public class Teacher extends Person{
}
public class Application {
public static void main(String[] args) {
Person person = new Student();
person.go(); //此行会编译报错:
}
}
// 高 低
Person person = new Student();
//Student类中才有go方法,而此时Person声明为person类,不可用子类的方法,应强制转换person对象为Student类才能用go方法
即:
Student student = (Student) person;//强制转换
student.go();
将两句汇总成一句:
((Student)person).go();
输出:
go
本质:子类Student占用1.5个内存,而父类Person占用1.0个内存,子类多的0.5个内存封装了子类独有的方法,即便student覆盖了person,但person内存不够大装不下go方法,所以调用不了go
向下强制转换语法:
( ( 目标子类类型 ) 当前待转换的对象名) . 子类方法();
( ( Student ) person ) . go( );
而子类转父类:低转高可默认,但是转换后可能会丢失自己本身原有的方法
Student student = new Student();
Person person = student ;//低转高
//此时转换后person不能再调用Student类的go方法
多态总结:
- 存在条件:父类引用指向子类的对象(反之不成立)
- 子类转换为父类:向上转型不需要强制转换,instanceof也会true
- 父类转换为子类:需要强制转换