它可能不相关,但这是在 Android 2.3.3 下,我使用 openjdk7。
我正在尝试使用对象的加速度值来计算对象的速度和位移,给定 3 个空间加上时间的坐标。为此,我创建了一个名为Coord4D
, 和Velocity
,的类,Displacement
并且Acceleration
是这个类的子类。像这样:
Coord4D
:
class Coord4D {
// Statics //
private static final long ONE_TO_NANO_FACTOR = 1000000000;
// Members //
private long key;
private float xValue;
private float yValue;
private float zValue;
// ... more ...
}
和继承类的一个例子Velocity
:
public class Velocity extends Coord4D{
// ... members and methods and stuff ...
}
而现在的问题。在Coord4D
中,我概括了从导数中获得反导数的方法。例如,我想检索Velocity
给定两个Acceleration
实例(过去和当前)的Velocity
实例和前一帧的实例。
Displacement
当执行此操作的方法位于and时,一切正常Velocity
,但我发现它是多余的,因为两者都在计算完全相同的东西,而且我讨厌编写两次相同的代码行。所以我决定将这些方法移入Coord4D
,并使用这样的泛型:
protected static <Derivative extends Coord4D,
AntiDerivative extends Coord4D> AntiDerivative getIntegrationStep(
Derivative previous,
Derivative current,
AntiDerivative previousStep){
// Time is in nanoseconds, must change it in seconds
float dT = ( current.getTime() - previous.getTime() )
/ ONE_TO_NANO_FACTOR;
// Parameters have seconds for unit : ..., ?*s, ?, ?/s, ?/s², ...
float dX = current.getX() - previous.getX();
float dY = current.getY() - previous.getY();
float dZ = current.getZ() - previous.getZ();
// do the integration step and add the previous step value
float x = dX * dT + previousStep.getX();
float y = dY * dT + previousStep.getY();
float z = dZ * dT + previousStep.getZ();
return (AntiDerivative) new Coord4D(current.getTime(), x, y, z);
}
并且Velocity
andDisplacement
对象具有此方法(示例来自Velocity
)。
public static Velocity getVelocity(Acceleration previousAccel,
Acceleration currentAccel,
Velocity initialVelo) {
return getIntegrationStep(previousAccel,
currentAccel,
initialVelo);
}
现在,出于某种原因,这条线
return getIntegrationStep(previousAccel, currentAccel, initialVelo);
导致一个ClassCastException
. 我不明白为什么,因为我认为我对泛型的逻辑很好。有人可以帮我找出缺陷在哪里吗?
堆栈跟踪(来自 LogCat):
W/dalvikvm(8891): threadid=1: thread exiting with uncaught exception (group=0x4001d648)
E/AndroidRuntime(8891): FATAL EXCEPTION: main
E/AndroidRuntime(8891): java.lang.ClassCastException: me.aybabt.android.prototypes.physics.Coord4D
E/AndroidRuntime(8891): at me.aybabt.android.prototypes.physics.Velocity.getVelocity(Velocity.java:40)
E/AndroidRuntime(8891): at me.aybabt.android.prototypes.AcceleratorActivity.onSensorChanged(AcceleratorActivity.java:199)
E/AndroidRuntime(8891): at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:539)
E/AndroidRuntime(8891): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(8891): at android.os.Looper.loop(Looper.java:150)
E/AndroidRuntime(8891): at android.app.ActivityThread.main(ActivityThread.java:4293)
E/AndroidRuntime(8891): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(8891): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(8891): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
E/AndroidRuntime(8891): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
E/AndroidRuntime(8891): at dalvik.system.NativeStart.main(Native Method)