[英]Using moment of inertia in jscience
我正在制作一個簡單的物理計算器中使用 jscience。 給定一些齒輪和旋轉圓柱體,我需要計算慣性矩。
我更喜歡使用 jscience,但似乎 jscience 沒有慣性矩的度量? 或者慣性矩是否表示為其他東西? 從這些公式我得到慣性矩可以用 kg*m^2 來描述。
查看jscience中的其他數量接口,我嘗試模仿“Mass”接口並創建了自己的名為“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 中定義慣性矩嗎?
我對 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));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.