0

我在我正在制作的一个简单的物理计算器中使用 jscience。给定一些齿轮和旋转气缸,我需要计算惯性矩。

我更喜欢使用 jscience,但似乎 jscience 没有惯性矩的度量?或者惯性矩是否表示为其他东西?从这些公式中,我得到惯性矩可以用 kg*m^2 来描述。

查看 jscience 中的其他量接口,我尝试模仿“质量”接口并创建了自己的量接口,名为“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 more

TLDR:(如何)我可以在 jscience 中定义惯性矩吗?

4

1 回答 1

1

我对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,该值当前为空,因为尚未分配。

因此,类似以下的内容可能会起作用:

public interface MomentOfInertia extends Quantity {

    public final static Unit<MomentOfInertia> UNIT = 
        new ProductUnit<MomentOfInertia>(SI.KILOGRAM.times(SI.SQUARE_METRE));

}
于 2015-11-11T08:46:11.413 回答