我不确定在我的示例程序 53,00 中树(参考变量)如何成为对象树的实例?我期待“Pine”和“oops”作为输出,但为什么“Tree”包含在输出中?我根本没有给 Tree tree = new Tree() 。
class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class forrest {
public static void main( String[] args )
{
Tree tree = new Pine();
if( tree instanceof Pine )
System.out.println( "Pine" );
if( tree instanceof Tree )
System.out.println( "Tree" );
if( tree instanceof Oak )
System.out.println( "Oak" );
else System.out.println( "Oops" );
}
}