我做了很多Java編程,然後放棄它,並做了一堆紅寶石。現在我回到了Java,我想知道我的編程風格是否奇怪。Java編程風格建議
我的下面的代碼對我來說感覺非常冗長,問題是它是否合理的慣用Java? 你會推薦的任何建議/改進?
final static int FORCE_RIGHT = 0; final static int FORCE_DOWN = 1; final static int FORCE_LEFT = 2; final static int FORCE_UP = 3; final static int IMP_RIGHT = 4; final static int IMP_DOWN = 5; final static int IMP_LEFT = 6; final static int IMP_UP = 7; public void applyForce(int dir) { counter++; Vector2 vect = new Vector2(); switch (dir) { case FORCE_RIGHT: vect = new Vector2(3.0f, 0.0f); break; case IMP_RIGHT: vect = new Vector2(1.0f, 0.0f); break; case FORCE_LEFT: vect = new Vector2(-3.0f, 0.0f); break; case IMP_LEFT: vect = new Vector2(-1.0f, 0.0f); break; case FORCE_UP: vect = new Vector2(0.0f, -3.0f); break; case IMP_UP: vect = new Vector2(0.0f, -1.0f); break; case FORCE_DOWN: vect = new Vector2(0.0f, 3.0f); break; case IMP_DOWN: vect = new Vector2(0.0f, 1.0f); break; } Vector2 place = body.getWorldCenter(); if (dir == FORCE_RIGHT || dir == FORCE_LEFT || dir == FORCE_DOWN || dir == FORCE_UP) { body.applyForce(vect, place); } else { body.applyLinearImpulse(vect, place); } Log.v("CAR", "Applied force: " + dir + "("+counter+")"); }
謝謝,各種好主意。 – pitosalas