instanceof和类型转换

public static void main(String[] args) {
//Object >String
//Object > Person >Student
//Object > Person >Teacher

Object object =new Student();

//System.out.println(X instanceof Y);//能不能编译通过,有没有父子关系

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
}

public static void main(String[] args) {
//类型之间的转换:父 子

//高 低
Person obj = new Student();

Student student= (Student)obj;
student.go();

/*
1.父类引用指向子类的对象
2.把子类转换为父类,向上转换
3.把父类转换为子类,向下转换:强制转换
4.方便方法的调用,减少重复代码的复用

*/
}

posted @ 2021-07-26 19:21  清钦  阅读(39)  评论(0)    收藏  举报