2

在我正在制作的应用程序中,我需要一些物理计算(功率、力、扭矩等)。我想使用 JScience,因为它在跟踪单位时似乎非常有用。例如,要分配速度,我会这样做:

Amount<Velocity> vel = Amount.valueOf(100, SI.METERS_PER_SECOND);

我的问题是我想在课堂上分配一些角速度,但我无法理解我应该如何做到这一点。假设我想指定 100 rad/s 的角速度。

Amount<AngularVelocity> angVel = Amount.valueOf(100, {SOME UNIT});

我在 JScience 中找不到任何用于角速度的单位。文档说“这个数量的系统单位是”rad/s“(弧度每秒)。”但没有像“SI.RAD_PER_SECOND”这样的东西或任何像我认为的枚举值......

所以问题是:我在后面的代码示例中写成{SOME UNIT}什么?

Birger 的欢呼

---编辑--

有人建议使用“SI.RADIAN.divide(SI.SECOND)”作为单位,但是我必须将变量分配为不同的类型。

// This will not compile, due to type mismatch
Amount<AngularVelocity> angVel = Amount.valueOf(100, SI.RADIAN.divide(SI.SECOND));

// This will compile, but I want to assign the amount as a AngularVelocity ...
Amount<?> angVel = Amount.valueOf(100, SI.RADIAN.divide(SI.SECOND));

// Another, working alternative. But, surely, there is a way to assign an angular velocity using rad/s ... ?
Amount<Frequency> angVel = Amount.valueOf(100 * Math.PI, SI.HERTZ);
4

1 回答 1

2

我不是这个特定库的专家,但是 eclipse 中的一些快速测试让我明白了这一点:

Amount<AngularVelocity> angVel = Amount.valueOf(100, AngularVelocity.UNIT);

问题在于,如果 AngularVelocity.UNIT 发生变化,您的数量也会发生变化。但我不太可能这么说。

否则,给出一个不太漂亮的代码行:

Amount<AngularVelocity> angVel = Amount.valueOf(100, SI.RADIAN.divide(SI.SECOND).asType(AngularVelocity.class));

这让我在使用 Java 编程时得到了一个很好的提示:使用控制空间。很多。我从来没有使用过这个库,我仍然能够通过快速检查 AngularVelocity.UNIT 版本的文档来找到它。另一个只是您自己的解决方案之后的控制空间。

于 2015-10-14T13:38:59.123 回答