English 中文(简体)
Java对宏观标准的平均计算
原标题:Equivalent of #define in Java for macros
最佳回答

如果你有一系列的双倍,而且每个具体职位都有一个明确的含义,那么你就应该建立一个阶层。

public class MyParamBlob extends ParentParamBlob
{
    private double myReadableParameter;
    private double anotherParameter;
    private double yetOneMore;

    // getters and setters as appropriate
}

如果你需要处理现有的<代码>(,从“外面”中可以找到一种构造和(或)一种计数阵列的方法。

public class MyParamBlob
{
    ...
    public MyParamBlob(double[] values)
    {
        setAll(values);
    }
    // getters and setters as appropriate
    ...
    public void setAll(double[] values)
    {
        myReadableParameter = values[0];
        anotherParameter = values[1];
        // etc.
    }
}

Edit - to explain my comment

If the original parent class that this is a subclass of (the reason the double[] exists in the first place) has a getter for the array, that could be overridden in this class, building and returning the array when requested -- e.g.

public double[] getParams()
{
    double[] params = new double[4];
    params[0] = myReadableParameter;
    params[1] = anotherParameter;
    // etc.
}

如果阵列为直接 父母类别的实例,如myArray = 母体内,<<>m/code>或 double d2 = 母体内,, 则 (1) 坏设计和(2) 打电话者可更改在oulInstance.params?

问题回答

是的,这完全可以通过不使用一个阵列来实现:

double myReadableParameter;
double anotherReadableParameter;

如果你需要把他们作为收藏品,你总是能够将他们列入一个清单。

是否有任何理由?

...
public double getMyReadableParam() {
    return params[0];
}

public void setMyReadableParam(double value) {
    params[0] = value;
}
...

缩略语

enum Param {
    AGE(0);
    WEIGHT(1);
    ...

    private int arrayIndex;
    // Constructor
    Param(int index) {
        arrayIndex = index;
    }

    public double getValue(double[] params) {
        return params[arrayIndex];
    }
}

以及

double age = Param.AGE.getValue(params);

我认为这比其他建议好,但我要表明如何使用<条码>ENUM<>>。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...