I want to have an abstract class like this:
public abstract Operator {
public int[] operands;
public Operator(int[] operands) {
this.operands = operands;
}
public abstract int getOperatorResult();
}
And some operators to extend from it like:
public class Add extends Operator {
public Add(int[] operands) {
super(operands);
}
public int getOperatorResult() {
return operands[0] + operands[1];
}
}
And an OperatorCreator which gets an expression like "1+1" and returns the appropriate Operator's sub-class(in this case Add):
public class OperatorCreator {
public static Operator getInstance(String expr) {
...
}
}
The main problem: And I want to let others to design their own operators by extending Operator class. And use their operators polymorphically. something like this:
public class Main {
public static void main(String[] args) {
Operator op = OperatorCreator.getInstance("1-2");
System.out.println(op.getOperatorResult());
}
}
How can I do this? Suppose that I know the location of .class file in which the new Operator's sub-class is compiled.
Thanks!
Aucun commentaire:
Enregistrer un commentaire