2

この例では:

if (object instanceof SomeThing || object instanceof OtherThing) {
    System.out.println("this block was entered because of: " + **____** )
}

真の条件が SomeThing か OtherThing かを確認できますか?

編集:条件の分離を避けようとしています。

ありがとう。

4

5 に答える 5

0

三項演算子を使用してみてください:

 if (object instanceof SomeThing || object instanceof OtherThing) {
        System.out.println("this block was entered because of: " 
           + (object instanceof SomeThing? "SomeThing" : "OtherThing") );
    }
于 2013-07-05T11:20:08.713 に答える
0
System.out.println("This block was entered because object is instance of SomeThing or OtherThing."); 

別の方法として:

System.out.println("This block was entered because object is " + object.getClass().getName());

または、非常に読みにくい:

boolean other = false;
if (object instanceof SomeThing || (other = true) || ....) {
    System.out.println("because of " + other?"OtherThing":"SomeThing")
}
于 2013-07-05T11:30:58.667 に答える