有没有办法以类似的方式一次初始化多个对象:
SimpleDoubleProperty sx, sy, vx, vy, dt, ro, w, angle, steps;
SimpleDoubleProperty[] prop = {sx,sy,vx,vy,dt,ro,w,angle,steps};
double[] initial = {0,0,10,10,0.1,1,1,60,20};
for(int i=0; i<prop.length; i++){
prop[i] = new SimpleDoubleProperty(initial[i]);
}
主要目标是能够自己使用sx,sy ..变量并缩短代码。 我知道我现在已经覆盖了那些变量(它们不再存储在这个数组中):
prop[i] = new SimpleDoubleProperty(initial[i]);
但这只是一个更好地解决这个问题的例子。
double[] initial = {0,0,10,10,0.1,1,1,60,20};
sx = new SimpleDoubleProperty(initial[0]);
sy = new SimpleDoubleProperty(initial[1]);
vx = new SimpleDoubleProperty(initial[2]);
vy = new SimpleDoubleProperty(initial[3]);
dt = new SimpleDoubleProperty(initial[4]);
ro = new SimpleDoubleProperty(initial[5]);
w = new SimpleDoubleProperty(initial[6]);
angle = new SimpleDoubleProperty(initial[7]);
steps = new SimpleDoubleProperty(initial[8]);
还有其他办法吗?