我在一个简单的物理计算器中使用jscience。我需要计算一些齿轮和旋转圆柱的惯性时刻。
我更喜欢使用jscience,但似乎jscience没有测量转动惯量吗?或者,转动惯量是否表示为其他东西?从这些公式中,我得到转动惯量可以用kg*m^2来描述。
查看jscience中的其他quantity接口,我尝试模仿"Mass“接口,并创建了自己的quantity接口"MomentOfInertia":
package jscience;
import javax.measure.quantity.Quantity;
import javax.measure.unit.Unit;
public interface MomentOfInertia extends Quantity {
public final static Unit<MomentOfInertia> UNIT =
SI.KILOGRAM.times(SI.SQUARE_METRE).asType(MomentOfInertia.class);
}接下来,我试图定义一个转动惯量:
public static void main(String[] args) throws Exception {
Amount<MomentOfInertia> moi = Amount.valueOf(1000,
SI.KILOGRAM.times(SI.SQUARE_METRE).asType(MomentOfInertia.class));
System.out.println(moi);
}但是,如果不抛出以下异常,这将无法运行:
Exception in thread "main" java.lang.ExceptionInInitializerError
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:142)
at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
at java.lang.reflect.Field.get(Field.java:393)
at javax.measure.unit.Unit.asType(Unit.java:170)
at test.Test.main(Test.java:8)
Caused by: java.lang.NullPointerException
at javax.measure.unit.Unit.asType(Unit.java:174)
at jscience.MomentOfInertia.<clinit>(MomentOfInertia.java:10)
... 8 moreTLDR:(如何定义jscience中的转动惯量?)
发布于 2015-11-11 08:46:11
我不熟悉JScience,但看看Torque的定义方式:
public interface Torque extends Quantity {
public final static Unit<Torque> UNIT =
new ProductUnit<Torque>(SI.NEWTON.times(SI.METRE));
}这里存在的问题是周期性初始化:您调用asType来获取您将分配给MomentOfInertia.UNIT的值,但是asType(MomentOfInertia.class)需要MomentOfInertia.UNIT (当前为null ),因为它没有被分配。
因此,下面这样的内容可能会奏效:
public interface MomentOfInertia extends Quantity {
public final static Unit<MomentOfInertia> UNIT =
new ProductUnit<MomentOfInertia>(SI.KILOGRAM.times(SI.SQUARE_METRE));
}https://stackoverflow.com/questions/33646720
复制相似问题