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.方便方法的调用,减少重复代码的复用

*/
}

上一篇:源码5——const/new/instanceof/函数柯里化


下一篇:Java面向对象:instanceof 和类型转换