Fruit ---------------->
Apple大致是这样的,因为你的Fruit fruit 是局部变量,所以编译器认为,你需要的只是一个Fruit .所以就只能调用 fruit变量的方法。 如果要 调用 Apple 的方法,则只能判断类型再强转了使用了。if( fruit instanceof Apple){// 这就判断fruit是不是Apple的实例了。真正运行的时候指向的对象 Apple a2 = (Apple)fruit; a2.xxx();}// 至于为什么会这样,你考虑一下,假若,还没有Apple类,只有 Fruit类的时候,这可能的吧。那么,我有一个方法,接收一个Fruit 类...
可以这样理解,现在Pear, Orange也继承了Fruit。Fruit fruit = new Apple();fruit的类型是Fruit,它是不知道是Apple/Pear/Orange哪一个,所以fruit只能调用Fruit的方法。如果需要用Apple的方法,可以这样:Apple apple = (Apple)fruit; ...