【发布时间】:2012-07-16 08:07:33
【问题描述】:
我不确定树(引用变量)如何成为对象树的实例 我的示例程序 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" );
}
}
【问题讨论】:
-
我建议您开始将您的
if块包含在{...}s 中。我不确定if块是否符合您的预期。
标签: java instanceof