对象造型(引用类型转换)

1. Student继承于 Person类

     只有存在继承关系的对象才有资格进行强制类型转换

     Persion p = new Persion();

     Student s = (Student)p;//错的,2种不同的类型

   而:    

     Persion p1 = new Student();//多态

     Student s1 = (Student)p1;//可以,因为p1这个引用变量实际指向的就是Student对象,所以才可以

    上述是父类强制转成子类,而子类是可以直接转成父类,因为多态···

2.不相干的对象,是没办法进行强制类型转换的   

     Persion p = new Persion();

     Car c = (Car)p;//错误的

原文地址:https://www.cnblogs.com/tommy-huang/p/4254213.html