-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で作成する必要がありますか?また、セッターとゲッターの代わりにここで静的メソッドを使用するにはどうすればよいですか?

4

5 に答える 5

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);
}
于 2012-08-03T21:11:02.113 に答える
2

すべての演算 (加算、乗算、除算など) を Calculator クラスの静的メソッドにします。

   class Calculator{

     public static int add(int a, int b){
           return a+b;
     }
     ...
于 2012-08-03T21:11:22.557 に答える
1

操作を正しくモデル化していないため、メソッドはすべて間違っています。結果を含むことは想定されておらず、すべての操作ではなく、1 つの操作のみを実行する必要があります。操作オブジェクトは不変である必要があり、2 つのオペランドが指定された特定の操作に対する応答を生成する必要があります。二項演算も単項演算から分離する必要があります。

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);
于 2012-08-03T21:12:05.253 に答える
0

これは、列挙型の 1 つまたは 2 つの適切な使用方法のように見えます。

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)
    );
}
于 2012-08-03T21:19:09.223 に答える
0

まだ答えられていない質問の部分に答えるだけです:

Java ではプロトタイプは必要ありません。

于 2012-08-03T21:11:54.380 に答える