对象的类型转换
课程代码:
1 // 类型转换 2 // 高 低 3 Person obj = new Student(); 4 // 转成Student类型,就可以使用Student类的方法了 5 ((Student) obj).go(); 6 7 // 低 高 8 Student student = new Student(); 9 student.go(); 10 Person person = student;
注意点:
1:只能父类引用指向了子类的对象。反过来,不行。
2:把子类转换为父类,向上转型。可直接转。
3:把父类转换为子类,向下转型,需要强制转换。例:((Student) obj).go();

浙公网安备 33010602011771号