java.instanceof
package Demo.oop.APP.Demo05; public class application { public static void main(String[] args) { //object>String //Object>Person>student //Object>Person>Teacher Object object = new Student(); Person person=new Student(); System.out.println(object instanceof Student );//true System.out.println(object instanceof Person);//true System.out.println(object instanceof Teacher);//false System.out.println(object instanceof String);//false System.out.println("====================="); System.out.println(person instanceof Student);//ture System.out.println(person instanceof Teacher);//false //System.out.println(person instanceof String);//编译报错 } } /** * 1.父类引用指向子类对象 * 2.把子类转为父类,向上转型 * 3.把父类转换为子类,向下转换,强制转换 * 4.方便方法调用,减少代码重复 * 抽象-->封装,继承,多态 */
package Demo.oop.APP.Demo05; public class Person { }
package Demo.oop.APP.Demo05; public class Student extends Person{ }
package Demo.oop.APP.Demo05; public class Teacher extends Person{ }