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

     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;//错误的

相关文章:

  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
  • 2022-02-05
  • 2022-12-23
相关资源
相似解决方案