java 多种类型的使用实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15981402/
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 21:32:30  来源:igfitidea点击:

instanceof use for multiple types

javaintegerinstanceof

提问by Gedas

I am writing a TypeChecker for MiniJava and ExpOp needs to check if both of the entered expressions are of Integer to use plus, minus, times.

我正在为 MiniJava 编写 TypeChecker,ExpOp 需要检查输入的两个表达式是否都是整数以使用加号、减号、时间。

How can I write a line of code inside an ifstatement that includes both expressions and checks if both of them are instances of (instanceof) Integer?

如何在if包含两个表达式的语句中编写一行代码并检查它们是否都是 ( instanceof) 的实例Integer

This is what I have now:

这就是我现在所拥有的:

n.e1.accept(this) n.e2.accept(this) instanceof Integer

Appreciate your help.

感谢你的帮助。

采纳答案by acdcjunior

instanceofis a binary operator: it can only have twooperands.

instanceof是一个二元运算符:它只能有两个操作数。

The best solutionfor your problem is Java's boolean AND operator: &&.

您的问题的最佳解决方案是 Java 的布尔 AND 运算符:&&

It can be used to evaluate two boolean expressions: <boolean_exp1> && <boolean_exp2>. Will return trueif and only if both are trueat the time of the evaluation.

它可用于计算两个布尔表达式:<boolean_exp1> && <boolean_exp2>. true当且仅当两者都true在评估时才会返回。

if (n.e1.accept(this) instanceof Integer &&
    n.e2.accept(this) instanceof Integer) {
    ...
}

That being said, another possible solution is to cast them both inside a try/catchblock, and when one of them is not an Integera ClassCastExceptionwill be thrown.

话虽如此,另一种可能的解决方案是将它们都投射到try/catch块中,当其中一个不是Integera 时,ClassCastException将抛出。

try {
   Integer i1 = (Integer) n.e1.accept(this);
   Integer i2 = (Integer) n.e2.accept(this);
} catch (ClassCastException e) {
   // code reached when one of them is not Integer
}

But this is not recommended as it is a known anti-pattern called Programming By Exception.

但不建议这样做,因为它是一种称为Programming By Exception的已知反模式。

We can show you a thousand ways (creating methods, creating classes and using polymorphism) you can do that with one line, but none of them will be better or clearer than using the &&operator. Anything other than that will make you code more confusing and less maintainable. You don't want that, do you?

我们可以向您展示一千种方法(创建方法、创建类和使用多态),您可以用一行来完成,但没有一种比使用&&operator更好或更清晰。除此之外的任何事情都会使您的代码更加混乱且难以维护。你不想要那个,是吗?

回答by millimoose

You can make a utility function that uses the reflection counterpart of instanceof, Class.isInstance():

您可以使用反射对应一个效用函数instanceofClass.isInstance()

public static boolean allInstanceOf(Class<?> cls, Object... objs) {
    for (Object o : objs) {
        if (!cls.isInstance(o)) {
            return false;
        }
    }
    return true;
}

You use it like this:

像这样使用它:

allInstanceOf(String.class, "aaa", "bbb"); // => true
allInstanceOf(String.class, "aaa", 123); // => false

回答by Paul Chibulcuteanu

In case you have a situation where instanceofEITHER of the classes will suffice your needs you can use the ||(logical OR) operator:

如果您遇到类中的任一个instanceof满足您的需求的情况,您可以使用||(逻辑或)运算符:

if (n.e1.accept(this) instanceof Integer ||
n.e2.accept(this) instanceof Boolean) {
...
}