java - 尝试在另一个方法中实现 2 个方法时出错 - java

标签 java inheritance methods

我有 2 个方法 isArithmetric 和 isRelational,这两个方法都用于查看 e 是否属于某个类的某个实例(例如,如果 e 是像 2 + 3 这样的类型,那么它在 isArithmetric 中并且是 Additiona 的实例,如果 e 类似于 3 > 2,那么它在 isRelational 中并且是 GreaterThan 的实例)。我需要实现 isTypeCorrect 方法,我知道该方法使用上面提到的两种方法来查看使用时是否返回 true(如果 e 是上述两种方法之一),否则返回 false。问题是,我不知道如何实现 isTypeCorrect,我尝试从 isTypeCorrect 调用该方法并使用 if 语句,但它不起作用。任何帮助将不胜感激。

代码:

private boolean isArithmetic(Expression e) {
    return 
            e instanceof Addition 
        ||  e instanceof Subtraction
        ||  e instanceof Multiplication;
}

/**
 * sub-routine that can be used to check if this is type correct
 * true if e is one of the three classes, or their subclasses (if any)
 */
private boolean isRelation(Expression e) {
    return 
            e instanceof Equal 
        ||  e instanceof GreaterThan
        ||  e instanceof LessThan;
}

/**
 * If this collector is AddAll or TimesAll, then all stored expressions must be integer expressions
 * (i.e., Addition, Subtraction, Multiplication).
 * If this collection is ConjoinAll or DisjoinAll, then all stored expressions must be boolean expressions
 * (i.e., GreaterThan, LessThan, Equal)
 * @return whether it is possible to evaluate the stored collection of expressions.   
 */
boolean isTypeCorrect() {

}

最佳答案

看看这段代码。

boolean isTypeCorrect() {
    boolean result = false;
    AddAll a = new AddAll();
    TimesAll t = new TimesAll();
    if (this.isArithmetic(a) || this.isRelation(t)) {
        result = true;
    }

    return result;
}

at 分别是 AddAllTimesAll 的新实例,构造函数没有可更改的参数。每次调用此方法时它们怎么会有所不同。事实上,它们将是本地值,并在您退出该方法时超出范围。我不知道发生了什么,但看起来像是一个设计缺陷。

关于java - 尝试在另一个方法中实现 2 个方法时出错 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60766029/

相关文章:

到 1 的 Java 最短路径

java - 按钮单击操作

javascript - 如何通过构造函数将一个对象设置在另一个对象下

c++ - 继承时避免覆盖成员

python - a == b 为假,但 id(a) == id(b) 为真?

java - GridBagLayout 不接受我的 rowspan 命令(gridtheight)

java - 设计问题。为非法参数抛出已检查的异常

c++ - error LNK2019 未解析的外部符号虚拟类

python - 动态添加可调用类作为实例 "method"

java - 使用二维数组存储线的一部分