你能告诉我这条线是如何工作的....我的OperatorFactory.get(“add”)没有做任何事情。我没有收到任何印刷java接口...快速问题
ArithmeticOperator add = OperatorFactory.get ("add");
时,我有以下几点:
interface ArithmeticOperator {
// Returns the result of applying the operator to operands a and b.
double operate (double a, double b);
// Return a String that is the name of this operator.
String printName();
}
public class OperatorFactory implements ArithmeticOperator {
public OperatorFactory(){
}
public static ArithmeticOperator get(String name){
if(name.equals("add"))
return new PlusOperator();
else if(name.equals("sub"))
return new SubOperator();
else if(name.equals("mult"))
return new MultOperator();
else if(name.equals("div"))
return new DivOperator();
else
return null;
}
public double operate(double a, double b) {
throw new UnsupportedOperationException("Not supported yet.");
}
public String printName() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
public class PlusOperator extends OperatorFactory {
public double operate(double a, double b) {
return a + b;
}
public String printName() {
return "Add";
}
}
public class PlusOperator extends OperatorFactory {
public double operate(double a, double b) {
return a + b;
}
public String printName() {
return "Add";
}
}
你为什么要印一些东西?你只是建立它,而不是调用它的** printName()**方法.. – Jack 2010-03-11 14:58:52
“我没有得到任何印刷品”。那么,代码不会在任何地方做到这一点。你在哪里/如何印刷一些东西? – BalusC 2010-03-11 14:58:55
“我没有得到任何印刷品” - 你在哪里打印什么?我没有看到你的代码在这里 – 2010-03-11 14:59:04