Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
4 つの演算子 (+、-、、/) のいずれかをメソッドに渡し、渡された演算子に基づいて 2 つの整数に対して操作を実行させたいと考えています。
static int op(String oper) { eval = 8 oper 4; }
たとえば、 で呼び出すと、 andop("+");が追加されます。84
op("+");
8
4
今のところ、「;期待」の後に8.
;
私が使用すべき他の構文はありますか?一部のコードのサイズを削減しようとしています。
Java 8では、次のようなことができるようになります
foo(IntBinaryOperator oper) eval = oper.apply(8, 4);
それから
foo(Integer::sum); IntBinaryOperator times = (a,b)->a*b; foo(times); foo( (a,b)->a/b );