我正在Challenge24Solver
用 Java 构建一个类。逻辑本身起作用并按预期找到解决方案(使用任意数量的参数)。无论如何,项目的那一部分正如我所料。
问题来自解决方案的表示问题。公平地说,我已经用 Python 完成了这个项目,并决定尝试用 Java 作为一种介绍,这可能是问题所在,我也在尝试像 Python 一样做这件事。
以下是我的一些课程:
abstract class Operation { \\ Basic operation class
static String token;
abstract public double operate (double x, double y);
}
class AddOp extends Operation {
static String token = "+";
public double operate (double x, double y) {
return x+y;
}
}
//other operation classes SubOp, MulOp, DivOp
class Challenge24Step { // represents one step in a solution
Operation operator;
double x, y; //operands
double value; //the value of the step (x operator y)
public Challenge24Step (Operation operator, double x, double y) {
this.operator = operator;
// constructor code;
}
public String toString () {
return String.format("%s %s %s = %s", this.x, this.operator.token, this.y,
this.value);
}
}
问题是它仍然从 Operation 类中获取令牌:“null”
我知道这可能是因为运算符被声明为操作,但我不明白为什么它不通过标准继承:实例、类、每个超类
我将如何重新排列脚本以便使用更具体的操作类的标记?