我仍然是Java的新手,但我个人认为我的代码很草率,有太多的变量.我知道我可以将它们隐藏在方法或类中,但我听说将它们放在一个单独的类中是不好的做法,而且大多数都不是静态的.谁能给我一些提示?提前致谢!
public static float c = .2f; //cube dimensions
private float separation = 4.0f*c;
private float angle = 0f; //set to 0
private float rotate = 1.0f;
private float h_height = 1.0f;
private float h_width = 1.0f;
private float h_length = 1.0f;
private float b_height = 1.5f;
private float b_width = .5f;
private float b_length = 1.0f;
private float a_height = 1.5f;
private float a_width = .5f;
private float a_length = .5f;
private float l_height = 1.5f;
private float l_width = .5f;
private float l_length = .5f;
private float h_x_s = 0.0f*c;
private float h_y_s = 5.0f*c;
private float h_z_s = 0.0f*c;
private float h_x;
private float h_y;
private float h_z;
private float b_x_s = 0.0f*c;
private float b_y_s = 2.5f*c;
private float b_z_s = 0.0f*c;
private float b_x;
private float b_y;
private float b_z;
private float a_x_s = 1.5f*c;
private float a_y_s = 2.5f*c;
private float a_z_s = .0f*c;
private float a_x;
private float a_y;
private float a_z;
private float l_x_s = .5f*c;
private float l_y_s = -.5f*c;
private float l_z_s = 0.0f*c;
private float l_x;
private float l_y;
private float l_z;
private float a_rotate = 0f;
private float a_speed = .6f;
private float l_rotate = 0;
private float l_speed = .6f;
private double a_c;
private double l_c;
private double a_s;
private double l_s;
private float max_a = 30f; //angle
private float max_l = 30f; //angle
private boolean a_forward;
private boolean l_forward;
private float move_z = 0; //set to 0
private float speed_z = .01f*separation;
private float max_z = 2*separation;
private boolean forward_z;
Run Code Online (Sandbox Code Playgroud)
PS评论实际上并不是变量的用途,只是在调试时提醒我.此外,我的变量名称可能会遭到专业开发人员的极大反对,但这是我可以轻松跟踪的内容.就像我之前说的那样,这些变量中的大多数都在积极改变,并且在多种方法中用于此事,但是我可以隐藏一些我离开的方法.
看起来你的变量正在尖叫,让你把它们放在课堂上.
我只是根据你的变量名推断,但我几乎可以肯定这就是你所需要的.
例如,您可以实现3个类,如本答案底部的类.
这将允许您的变量声明看起来像这样:
变量声明:
Polygon polygonA = new Polygon(1.5, .5, .5);
Polygon polygonB = new Polygon(1.5, .5, 1.0);
Polygon polygonL = new Polygon(1.5, .5, .5);
Coordinate coordH = new Coordinate();
Coordinate coordB = new Coordinate();
Transform transformA = new Transform(0, .6);
Transform transformL = new Transform(0, .6);
Run Code Online (Sandbox Code Playgroud)
示例类定义:(每个都在一个单独的文件中)
class Polygon {
float height;
float width;
float length;
public Polygon(int h, int w, int l) {
height = h;
width = w;
length = l;
}
}
class Coordinate {
float x;
float y;
public Coordinate() {}
public Coordinate(float xCoord, float yCoord) {
x = xCoord;
y = yCoord;
}
}
class Transform {
float rotate;
float speed;
public Transform(float r, float s) {
rotate = r;
speed = s;
}
}
Run Code Online (Sandbox Code Playgroud)