Swa*_*nAH 10 java parameters methods
我试图将类A的选定"get"方法传递给B类中的方法.我已经将Java Pass Method作为参数检出,但是我无法以合理的方式采用接口方法解决我的问题.我宁愿不使用java 8(lambdas),如果可能的话也要避免反射.我的感觉是,我以错误的方式看待我的问题.以下是我要完成的具体简化示例:
我有一个包含一些字段和get方法的类:
public class DataStore {
private float a;
private float b;
private float c;
public float getA() {
return a;
}
public float getB() {
return b;
}
public float getC() {
return c;
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,我让我的主类实例DataStore化为Map的值,然后访问DataStore的特定字段,如:
public class App {
public static void main(String[] args) {
// declare TreeMap using DataStore class as value
Map<Integer, DataStore> dataMap = new TreeMap<Integer, DataStore>();
// populate Map with example data
dataMap.put(2, new DataStore(1f,2f,3f));
dataMap.put(10, new DataStore(3f,4f,5f));
dataMap.put(4, new DataStore(6f,7f,8f));
// work with specific fields in DataStore, e.g. assign to array
float[] aArray = getValuesAsArray(dataMap, DataStore.getA());
float[] bArray = getValuesAsArray(dataMap, DataStore.getB());
float[] cArray = getValuesAsArray(dataMap, DataStore.getC());
}
/**
* Assign specific field of DataStore from Map to Array
* @param dataMap
* @param getVar - reference for specified getter method
* @return
*/
private static float[] getValuesAsArray(Map<Integer, DataStore> dataMap, MethodReference getVar()) {
int i = 0;
int nMap = dataMap.size();
float[] fArray = new float[nMap];
for (Map.Entry<Integer, DataStore> entry : dataMap.entrySet()) {
DataStore ds = entry.getValue();
fArray[i] = ds.getVar();
i++;
}
return fArray;
}
}
Run Code Online (Sandbox Code Playgroud)
显然这不会起作用,因为我必须弄清楚如何将我选择的get方法传递给getValuesAsArray().不知怎的,我想,我的做法可能是错的.所以我愿意接受建议.
Era*_*ran 14
您的getX()方法可以看作是一个接受DataStore实例并返回float的Function.
在Java 8中,您可以使用方法引用来表示它们:
float[] aArray = getValuesAsArray(dataMap, DataStore::getA);
float[] bArray = getValuesAsArray(dataMap, DataStore::getB);
float[] cArray = getValuesAsArray(dataMap, DataStore::getC);
Run Code Online (Sandbox Code Playgroud)
然后你getValuesAsArray会接受一个Function<DataStore,Float>参数并执行函数:
private static float[] getValuesAsArray(Map<Integer, DataStore> dataMap, Function<DataStore,Float> func) {
int i = 0;
int nMap = dataMap.size();
float[] fArray = new float[nMap];
for (Map.Entry<Integer, DataStore> entry : dataMap.entrySet()) {
DataStore ds = entry.getValue();
fArray[i] = func.apply(ds);
i++;
}
return fArray;
}
Run Code Online (Sandbox Code Playgroud)
在不使用Java 8的情况下,您可以定义自己的接口,该接口包含接受DataStore实例并返回实例的方法float.然后,您不必使用Java 8方法引用,而是必须向您的getValuesAsArray方法传递该接口的实现(您可以使用实现接口的匿名类实例),该实现调用其中一个getX()方法.
例如 :
public interface ValueGetter
{
public float get (DataStore source);
}
float[] aArray = getValuesAsArray(dataMap, new ValueGetter() {public float get (DataStore source) {return source.getA();}});
float[] bArray = getValuesAsArray(dataMap, new ValueGetter() {public float get (DataStore source) {return source.getB();}});
float[] cArray = getValuesAsArray(dataMap, new ValueGetter() {public float get (DataStore source) {return source.getC();}});
Run Code Online (Sandbox Code Playgroud)
和
private static float[] getValuesAsArray(Map<Integer, DataStore> dataMap, ValueGetter func) {
int i = 0;
int nMap = dataMap.size();
float[] fArray = new float[nMap];
for (Map.Entry<Integer, DataStore> entry : dataMap.entrySet()) {
DataStore ds = entry.getValue();
fArray[i] = func.get(ds);
i++;
}
return fArray;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14875 次 |
| 最近记录: |