Helicopter h = (Helicopter) new Rotorcraft(); )以及 throws Runtime exception 类型 ClassCastException .
Helicopter h = (Helicopter) new Rotorcraft();
throws
Runtime exception
ClassCastException
基类:
public class Rotorcraft { protected final int height = 5; protected int fly(){ return height; } }
public class Helicopter extends Rotorcraft { private int height = 10; public int fly() { return super.height; } public static final void main(String[] a){ Helicopter h = (Helicopter) new Rotorcraft(); } }
最基本的问题是你想把它变成一个 Helicopter 不是的东西 Rotorcraft
Helicopter
Rotorcraft
你的意思是写:
Helicopter h = new Helicopter();
?