你能告訴我這條線是如何工作的....我的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