目标:
我想为不经常更改的数据实现一个硬编码查找表,但是当它确实更改时,我希望能够快速更新程序并重建。
计划:
我的计划是定义一个自定义数据类型,如下所示...
private class ScalingData
{
public float mAmount;
public String mPurpose;
public int mPriority;
ScalingData(float fAmount, String strPurpose, int iPriority)
{
mAmount = fAmount;
mPurpose = strPurpose;
mPriority = iPriority;
}
}
然后,在主类中,像这样对数组进行硬编码......
public static ScalingData[] ScalingDataArray =
{
{1.01f, "Data point 1", 1},
{1.55f, "Data point 2", 2}
};
但是,这不会构建。我一直看到消息"> Type mismatch: cannot convert from float[] to ScalingData
"。
怎样才能实现我的目标?
更新
到目前为止,我已经尝试实施这些建议,但仍然遇到错误......
代码如下所示:
public class CustomConverter
{
//Lookup Table
private static ScalingData[] ScalingDataArray =
{
new ScalingData(1.01f, "Data point 1", 1),
new ScalingData(1.55f, "Data point 2", 2)
};
//Constructor
CustomConverter()
{
//does stuff
}
//Custom Data type
private class ScalingData
{
public float mAmount;
public String mPurpose;
public int mPriority;
ScalingData(float fAmount, String strPurpose, int iPriority)
{
mAmount = fAmount;
mPurpose = strPurpose;
mPriority = iPriority;
}
}
}
硬编码数组的错误是
No enclosing instance of type CustomConverter is accessible.
Must qualify the allocation with an enclosing instance of type CustomConverter
(e.g. x.new A() where x is an instance of CustomConverter).
编辑... 根据以下答案完成解决方案
public class CustomConverter
{
//Lookup Table
private static ScalingData[] ScalingDataArray =
{
new ScalingData(1.01f, "Data point 1", 1),
new ScalingData(1.55f, "Data point 2", 2)
};
//Constructor
CustomConverter()
{
//does stuff
}
//Custom Data type
private static class ScalingData
{
public float mAmount;
public String mPurpose;
public int mPriority;
ScalingData(float fAmount, String strPurpose, int iPriority)
{
mAmount = fAmount;
mPurpose = strPurpose;
mPriority = iPriority;
}
}
}
你不能在 Java 中这样做。您需要像这样使用构造函数:
public static ScalingData[] ScalingDataArray =
{
new ScalingData(1.01f, "Data point 1", 1),
new ScalingData(1.55f, "Data point 2", 2)
};
数组包含ScalingData,因此您必须添加这些实例。
public static ScalingData[] ScalingDataArray = {
new ScalingData(1.01f, "Data point 1", 1),
new ScalingData(1.55f, "Data point 2", 2)
};
顺便说一句:除非你真的需要,否则我不会用float
代替double
。大多数情况下,具有额外的精度比您保存的几个字节更有用。
正如其他人已经写的那样,您需要显式创建对象的实例,也就是说,您的数组需要诸如new ScalingData(1.01f, "Data point 1", 1)
之类的项目而不是{1.01f, "Data point 1", 1}
。这是语言语法的问题。
对于您的扩展问题,嵌套类ScalingData
应声明为 static
,否则,正如错误消息所说,它需要从它嵌套的类的封闭实例中创建,CustomConverter
(或者您必须使用此类型的实例调用运算符 new,如下所示: myCustomConverterInstance.new ScalingData(1.01f, "Data point 1", 1)
- 但ScalingData
不需要封闭类,只需将其声明为静态或将其声明为非嵌套类(。
如果你的数组有很多点,并且你发现类的名称有点太长,作为速记,你可以创建一个名称非常短的帮助程序函数,它只创建一个实例。稍长的名称有利于提高可读性,但长数组的初始化有时可能是一个例外。在这种名称只是ScalingData
的特殊情况下,增益并不大,但是对于某些类名,我确实发现此解决方案可以使代码更好,尤其是在数组很长的情况下。这只是一个黑客,但它允许数组初始化代码更短,可能更清晰:
protected static ScalingData point(float fAmount, String strPurpose, int iPriority) {
return new ScalingData(fAmount,strPurpose,iPriority);
}
public static ScalingData[] ScalingDataArray = {
point(1.01f, "Data point 1", 1),
point(1.55f, "Data point 2", 2),
...
};
而不是
public static ScalingData[] ScalingDataArray = {
new ScalingData(1.01f, "Data point 1", 1),
new ScalingData(1.55f, "Data point 2", 2),
...
};
附带说明一下,通常最好遵循 Java 命名约定,这意味着数组的名称应以小写字母开头 - scalingDataArray
而不是ScalingDataArray
(大写优先表示类(。
你不能指望一些随机数据神奇地转换为对象。
您需要实例化缩放数据对象。
我认为枚举可能更适合您的需求:
enum ScalingData{
DataPoint1(1.01f, "Data Point 1", 1),
DataPoint2(1.55f, "Data Point 2", 2);
final float Amount;
final String Purpose;
final int Priority;
ScalingData(float fAmount, String strPurpose, int iPriority){
Amount = fAmount;
Purpose = strPurpose;
Priority = iPriority;
}
}