public class Person1 {
public void run(){
System.out.println("run");
}
}
public class Student1 extends Person1{
public void go(){
System.out.println("go");
}
}
public class Application1 {
public static void main(String[] args) {
//高 低(低(Student类)到高(Person)可直接转)
Person1 obj = new Student1();
//子类转换成父类,可能丢失自己本来的一些方法
//bjo将这个对象转换为Student1类型,就可以使用Student1类型的方法了
((Student1)obj).go();//强制转换(高到低)(父类引用子类方法)
/*
Student1 bjo = (Student1) obj;//强制转换(高到低)
bjo.go();
*/
/*方法转换
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型
3,把父类转换为子类,向下转型:强制转换
4,方便方法的调用,减少重复的代码,简洁
抽象的三大特性:封装,继承,多态!
*/
}
}
/* instanceof 用法:来判断是否可以转型
//System.out.println(X instanceof Y);//能否通过编译通过,就看XY是否有父子(继承)关系
//Object > String
//Object > Person1 > Teacher1
//Object > Person1 > Student1
Object object = new Student1();
System.out.println(object instanceof Student1);//true
System.out.println(object instanceof Person1);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof String);//false
System.out.println(object instanceof Teacher1);//false
System.out.println("=============================");
Person1 person = new Student1();
System.out.println(person instanceof Student1);//true
System.out.println(person instanceof Person1);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher1);//false
//System.out.println(Person1 instanceof String);//编译报错!!
System.out.println("=============================");
Student1 student = new Student1();
System.out.println(student instanceof Student1);//true
System.out.println(student instanceof Person1);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher1);//编译报错
//System.out.println(Person1 instanceof String);//编译报错!!
System.out.println("=============================");
*/