1.父类对象可以强制转换为子类对象,但是前提是此父类对象为子类对象实例化的结果。

e.g. Fruit fruit=new Apple();

     Apple a=(Apple)fruit;//ok

e.g. 假设Apple类继承于Fruit类,

     Object fruit=new Fruit();

     Object apple=(Apple)fruit;//wrong

2.子类的实例向父类转型是为了实现多态。

相关文章:

  • 2022-02-25
  • 2021-07-02
  • 2021-07-15
  • 2021-12-04
猜你喜欢
  • 2021-06-09
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2022-01-07
  • 2021-06-12
相关资源
相似解决方案