この例では:
if (object instanceof SomeThing || object instanceof OtherThing) {
System.out.println("this block was entered because of: " + **____** )
}
真の条件が SomeThing か OtherThing かを確認できますか?
編集:条件の分離を避けようとしています。
ありがとう。
この例では:
if (object instanceof SomeThing || object instanceof OtherThing) {
System.out.println("this block was entered because of: " + **____** )
}
真の条件が SomeThing か OtherThing かを確認できますか?
編集:条件の分離を避けようとしています。
ありがとう。
三項演算子を使用してみてください:
if (object instanceof SomeThing || object instanceof OtherThing) {
System.out.println("this block was entered because of: "
+ (object instanceof SomeThing? "SomeThing" : "OtherThing") );
}
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")
}