我有 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;
}
a
和 t
分别是 AddAll
和 TimesAll
的新实例,构造函数没有可更改的参数。每次调用此方法时它们怎么会有所不同。事实上,它们将是本地
值,并在您退出该方法时超出范围。我不知道发生了什么,但看起来像是一个设计缺陷。
关于java - 尝试在另一个方法中实现 2 个方法时出错 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60766029/