我仍然是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;
P.S。评论实际上并不是变量的用途,只是在调试时提醒我。此外,我的变量名称可能会遭到专业开发人员的极大反对,但这是我可以轻松跟踪的内容。就像我之前说的那样,这些变量中的大多数都在积极改变,并且在多种方法中用于此事,但是我可以隐藏一些我可以隐藏起来的。
答案 0 :(得分:7)
看起来你的变量尖叫让你把它们放在课堂上。
我只是根据您的变量名称进行推断,但我几乎可以肯定这就是您所需要的。
例如,您可以实现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);
示例类定义:(每个都在一个单独的文件中)
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;
}
}
答案 1 :(得分:4)
我不确定你在哪里知道没有完成将变量放在不同的类中。我不知道你的代码的意图,但我想它与立方体有关。
您似乎有四种类型的变量:h
,b
,a
和l
,它们与非常类似。你应该将它们抽象为一个类。像这样:
public class Cube
{
private float height;
private float width;
private float length;
private float x_s;
private float y_s;
private float z_s;
private float x;
private float y;
private float z;
// Add getters, setters, constructors, etc...
}
然后您可以将当前代码减少到:
private Cube h;
private Cube b;
private Cube a;
private Cube l;
相当整洁,对吧?
您可以进一步将坐标和大小抽象为Vector3
对象:
public class Vector3
{
private float x;
private float y;
private float z;
}
制作Cube
:
public class Cube
{
private Vector3 size;
private Vector3 position_s;
private Vector3 position;
}
或者,如果大小和位置确实不同,您甚至可以为X,Y,Z坐标创建Point
类,为Size
创建height
类, length
,width
值。
顺便说一下,你真的应该使用有意义的变量名。 h
,a
,b
,l
和x_s
等内容对我没有任何意义。如果你在一段时间后查看你的代码,对你来说也没什么意义。