java instanceof 的反面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11181853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:05:56  来源:igfitidea点击:

The opposite of instanceof

javainstanceof

提问by Lolmister

Is it possible to get the opposite of instanceof in java? I have tried code like this:

是否有可能在java中获得instanceof的反面?我试过这样的代码:

if( example !instanceof blarg)....

but it won't let me put the ! anywhere without an error, please help.

但它不会让我把!任何地方都没有错误,请帮忙。

回答by Corbin

You have to negate the entire thing:

你必须否定整个事情:

if(!(example instanceof blarg))

回答by Ayaz Alifov

As an alternative make use of isInstancemethod:

作为替代使用isInstance方法:

   if (!example.class.isInstance(blarg))
   {
     // your code here
   }