我在想是否存在更好/更好的方法来否定 Java 中的 instanceof
。实际上,我正在做类似的事情:
if(!(myObject instanceof SomeClass)) { /* do Something */ }
但我认为应该存在一个“漂亮”的语法来做到这一点。
有谁知道它是否存在,以及语法是什么样的?
编辑:美丽,我可能会这样说:
if(myObject !instanceof SomeClass) { /* do Something */ } // compilation fails
instanceof
的优先规则...
boolean strIsString = str instanceof String;
...
不,没有更好的方法;你的是规范的。
我不知道当你说“美丽”时你会想到什么,但是这个呢?我个人认为它比您发布的经典表格更糟糕,但有人可能会喜欢它......
if (str instanceof String == false) { /* ... */ }
!= true
而不是 == false
:D
if(!(str instanceof String))
是唯一正确的方法,我需要停止思考替代方案
!
字符很容易被忽视......而且我想说有很多“我们”,例如 StringUtils 提供 isBlank()
和 {3 } 与 !isBlank()
相比...
您可以使用 Class.isInstance
方法:
if(!String.class.isInstance(str)) { /* do Something */ }
...但它仍然被否定并且非常丑陋。
this method returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses)
通常,您不仅需要 if
,还需要 else
子句。
if(!(str instanceof String)) { /* do Something */ }
else { /* do something else */ }
可以写成
if(str instanceof String) { /* do Something else */ }
else { /* do something */ }
或者您可以编写代码,这样您就不需要知道它是否为字符串。例如
if(!(str instanceof String)) { str = str.toString(); }
可以写成
str = str.toString();
else
为您的 if
,即使它只是一个无操作”,我称之为糟糕的建议。我现在意识到我看错了,但想放弃这个笔记,以防其他人也看错了。很好的答案!
如果您可以使用静态导入,并且您的道德准则允许它们
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);
}
}
接着...
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 做这样的事情:
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
}
if (Predicate.not(isInstanceOfTheClass).test(myObject)) { ...
。不是更好,imo,但应该工作!
好吧只是我的两分钱,使用一个是字符串方法:
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))
和整个条件来添加条件,就像下面代码片段中提到的方式一样。
if(!(str instanceof String)) { /* do Something */ } // COMPILATION WORK
代替
if(str !instanceof String) { /* do Something */ } // COMPILATION FAIL
我同意在大多数情况下 if (!(x instanceof Y)) {...}
是最好的方法,但在某些情况下创建一个 isY(x)
函数以便您可以if (!isY(x)) {...}
是值得的。
我是一个打字稿新手,在过去的几周里,我多次遇到这个 S/O 问题,所以对于谷歌人来说,打字稿的方法是创建一个这样的 typeguard:
typeGuards.ts
export function isHTMLInputElement (value: any): value is HTMLInputElement {
return value instanceof HTMLInputElement
}
用法
if (!isHTMLInputElement(x)) throw new RangeError()
// do something with an HTMLInputElement
我想这可能适用于 typescript 而不是常规 js 的唯一原因是 typeguard 是一种常见的约定,因此如果您为其他接口编写它们,那么为类编写它们也是合理/可理解/自然的。
有更多关于用户定义类型保护的详细信息,例如 in the docs
不定期副业成功案例分享