2

我对 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;

PS评论实际上并不是变量的用途,只是在调试时提醒我。此外,我的变量名称可能会遭到专业开发人员的反对,但我可以轻松地跟踪它。就像我之前说的,这些变量中的大多数都在积极变化,并在多种方法中使用,但有一些我不在,我可以躲起来。

4

2 回答 2

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;
    }
}
于 2013-06-08T00:23:54.953 回答
4

我不确定您从哪里得到将变量放在单独的类中的想法。我不知道您的代码的意图,但我想它与多维数据集有关。

您似乎有四种类型的变量:h、和b,它们非常相似。您应该将它们抽象为一个类。像这样的东西:al

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;
}

或者,如果大小和位置真的是不同的东西,你甚至可以Point为 X、Y、Z 坐标创建一个Size类,为height, length,width值创建一个类。

顺便说一句,你真的应该使用有意义的变量名。像h, a,b和之类的l东西x_s对我来说没有任何意义。如果您在一段时间后查看您的代码,它对您也没有任何意义。

于 2013-06-08T00:30:06.900 回答