Best way to “negate” an instanceof
我在想,是否存在一种更好/更巧妙的方法来否定Java中的instanceof。
实际上,我正在执行以下操作:
1
| if(!(str instanceof String)) { /* do Something */ } |
但是我认为应该存在一种"美丽"的语法。
有谁知道它是否存在以及其语法如何?
编辑:
美丽地说,我可能会这样说:
1
| if(str !instanceof String) { /* do Something */ } // compilation fails |
-
我非常讨厌instanceof的优先规则...
-
您总是可以创建一个变量,例如boolean strIsString = str instanceof String; ...
-
是的@Baqueta,是一个选择。 但是,以一种或另一种语法在内存使用方面可能会发生什么差异?
-
这个建设性评论如何?
-
Java创建者可以引入一个新关键字:notinstanceof。 只是我的两分钱^^
-
你举的例子是不好的,因为字符串不能有子类,所以你的例子基本上是一个参考isNull检查。
不,没有更好的办法。你的是规范的。
我不知道您说"美丽"时的想法,但是那又如何呢?我个人认为这比您发布的经典表格差,但有人可能会喜欢...
1
| if (str instanceof String == false) { /* ... */ } |
-
关于双重逻辑,可以使用!= true代替== false:D
-
看到这一点有助于我理解if(!(str instanceof String))是唯一正确的方法,我需要停止思考替代方法
-
我喜欢此解决方案,因为在阅读时不需要构建金属堆栈!
您可以使用Class.isInstance方法:
1
| if(!String. class. isInstance(str )) { /* do Something */ } |
...但是它仍然被否定并且非常丑陋。
-
好一点了,多余的括号使代码难看,恕我直言。
-
这不是慢很多吗?
-
这有不同的行为。 instanceof关键字包括子类,该方法不包括,您需要使用Class.isAssignableFrom复制行为。
-
@ChrisCooper这不是真的:this method returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses)
通常,您不只需要if,还需要else子句。
1 2
| if(!(str instanceof String)) { /* do Something */ }
else { /* do something else */ } |
可以写成
1 2
| if(str instanceof String) { /* do Something else */ }
else { /* do something */ } |
或者,您可以编写代码,这样就无需知道它是否为String。例如
1
| if(!(str instanceof String)) { str = str. toString(); } |
可以写成
如果您可以使用静态导入,并且您的道德准则允许
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class ObjectUtils {
private final Object obj ;
private ObjectUtils (Object obj ) {
this. obj = obj ;
}
public static ObjectUtils thisObj (Object obj ){
return new ObjectUtils (obj );
}
public boolean isNotA (Class < ? > clazz ){
return !clazz. isInstance(obj );
}
} |
接着...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import static notinstanceof. ObjectUtils. *;
public class Main {
public static void main (String[] args ) {
String a ="";
if (thisObj (a ). isNotA(String. class)) {
System. out. println("It is not a String");
}
if (thisObj (a ). isNotA(Integer. class)) {
System. out. println("It is not an Integer");
}
}
} |
这只是一个流畅的界面练习,我绝对不会在现实生活中使用它!
以您的经典方式走,它不会混淆其他人阅读您的代码!
如果您觉得它更容易理解,则可以使用Java 8执行以下操作:
1 2 3 4 5 6 7 8 9
| public static final Predicate<Object> isInstanceOfTheClass =
objectToTest -> objectToTest instanceof TheClass;
public static final Predicate<Object> isNotInstanceOfTheClass =
isInstanceOfTheClass.negate(); // or objectToTest -> !(objectToTest instanceof TheClass)
if (isNotInstanceOfTheClass.test(myObject)) {
// do something
} |
好的,只是我的两分钱,使用一个is字符串方法:
1 2 3 4 5 6 7 8 9 10
| public static boolean isString (Object thing ) {
return thing instanceof String;
}
public void someMethod (Object thing ){
if (!isString (thing )) {
return null;
}
log. debug("my thing is valid");
} |
您可以通过以下方式来实现。.只需添加括号if(!(condition with instanceOf))来添加条件,并在开始时添加!运算符即可添加整个条件,就像下面的代码片段中提到的那样。
1
| if(!(str instanceof String)) { /* do Something */ } // COMPILATION WORK |
代替
1
| if(str !instanceof String) { /* do Something */ } // COMPILATION FAIL |