2012-08-03 59 views
-1
public class Operations { 

    private int add; 
    private int sub; 
    private int mul; 
    private int div; 
    private double sqrt; 

    public void setadd(int a, int b) { 
     add = a + b; 
    } 

    public void setsub(int a, int b) { 
     sub = a - b; 
    } 

    public void setmul(int a, int b) { 
     mul = a * b; 
    } 

    public void setdiv(int a, int b) { 
     div = a/b; 
    } 

    public void setsqrt(double sqt) { 
     sqrt = Math.sqrt(sqt); 
    } 

    public int getadd() { 
     return add; 
    } 

    public int getsub() { 
     return sub; 
    } 

    public int getmul() { 
     return mul; 
    } 

    public int getdiv() { 
     return div; 
    } 

    public double getsqrt() { 
     return sqrt; 
    } 

} 

我必须做一个这样或Java的原型,这是没有必要的,我怎么在这里使用静态方法,而不是setter和getter ..我试图做一个计算器..我的方法是否可以?静态方法或setter或getters?

回答

2

让所有的操作(加法,乘法,除法等)计算器的静态方法类:

class Calculator{ 

    public static int add(int a, int b){ 
      return a+b; 
    } 
    ... 
2

我真的不明白设置和获取的角度来看,为什么不把你的计算器是这样的:

public class Calculator { 
public int add(int a, int b){ 
    return a + b; 
} 
public int sub(int a , int b){ 
    return a - b; 
} 
public int mul(int a, int b){ 
    return a * b; 
} 
public int div(int a, int b){ 
    return a/b; 
} 
public double sqrt(double sqt){ 
    return Math.sqrt(sqt); 
} 
+0

也可能是值得做这些静态的;你并不需要一个Calculator对象的实例来完成这些操作。 – 2012-08-03 21:14:36

+0

我认为即时消息只是在这里做实践,所有这些都只是学习和实验 – user1535963 2012-08-03 21:18:53

+0

这些方法真的应该是静态的 - 它们不在任何实例领域运行。 – 2012-08-03 21:19:01

0

只是回答没有回答但问题的一部分:

你不需要在Java中的原型。

1

你的方法都是错误的,因为你错误地模拟了你的操作。它不应该包含它的结果,它应该只做一个操作,而不是所有的操作。操作对象应该是不可变的,它应该给出给定两个操作数的特定操作的答案。您应该将二元运算与一元运算分开。

interface BinaryOp { 
    double calculate(double left, double right); 
} 
interface UnaryOp { 
    double calculate(double operand); 
} 
private static final BinaryOp ADD = new BinaryOp() { 
    double calculate(double left, double right) { 
     return left + right; 
    } 
}; 
private static final BinaryOp SUB = new BinaryOp() { 
    double calculate(double left, double right) { 
     return left - right; 
    } 
}; 
private static final BinaryOp MUL = new BinaryOp() { 
    double calculate(double left, double right) { 
     return left * right; 
    } 
}; 
private static final BinaryOp DIV = new BinaryOp() { 
    double calculate(double left, double right) { 
     return left/right; 
    } 
}; 
private static final UnaryOp SQRT = new UnaryOp() { 
    double calculate(double operand) { 
     return Math.sqrt(operand); 
    } 
}; 

现在,您可以按名称组织您的运营商:

private static final Map<String,BinaryOp> opByName = new HashMap<String,BinaryOp>(); 
static { 
    opByName.put("+", ADD); 
    opByName.put("-", SUB); 
    opByName.put("*", MUL); 
    opByName.put("/", DIV); 
} 

有了这个地图,你可以用你的行动来执行计算你:

String op = "+"; 
double left = 123; 
double right = 456; 
double res = opByName.get(op).calculate(left, right); 
0

这看起来像一个良好用于枚举或两个:

enum BinOp { 
    ADD { 
     @Override 
     public int eval(int leftArg, int rightArg) { 
      return leftArg + rightArg; 
     } 
     @Override 
     public String symbol() { 
      return "+"; 
     } 
    }, 
    SUBTRACT { 
     @Override 
     public int eval(int leftArg, int rightArg) { 
      return leftArg - rightArg; 
     } 
     @Override 
     public String symbol() { 
      return "-"; 
     } 
    } 
    // etc. 
    ; 
    public abstract int eval(int leftArg, int rightArg); 
    public abstract String symbol(); 
} 

和一元运营商的类似枚举(目前只有SQRT)。

如下,您可以用这些:

int left = 3; 
int right = 2; 
for (BinOp op : BinOp.values()) { 
    System.out.println("The value of " 
     + left + " " + op.symbol() + " " + right " is " 
     + op.eval(left, right) 
    ); 
} 
相关问题