我不确定树(引用变量)如何在我的示例程序53,00中成为对象Tree的实例?我期待"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" );
}
}
Run Code Online (Sandbox Code Playgroud)