我如何“得到”来自 Java 的 Scala 案例对象?

发布于 2024-08-27 08:51:22 字数 1952 浏览 11 评论 0原文

我在 Scala 中创建了一个案例对象层次结构,如下所示:

package my.awesome.package

sealed abstract class PresetShapeType(val displayName: String)

case object AccelerationSensor extends PresetShapeType("Acceleration Sensor")
case object DisplacementSensor extends PresetShapeType("Displacement Sensor")
case object ForceSensor        extends PresetShapeType("Force Sensor")
case object PressureSensor     extends PresetShapeType("Pressure Sensor")
case object StrainSensor       extends PresetShapeType("Strain Sensor")

我还有一段 Java 代码,我想在其中访问 PressureSensor,但以下代码不起作用:

package my.awesome.package.subpackage;

import my.awesome.package.PressureSensor;

// Do some stuff, then...

DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor, new Point3f(0,0,0));

那么,如何我是否从 Java 引用 PressureSensor 案例对象?我反编译了 PressureSensorPressureSensor$ 类的字节码,得到了以下结果:

Compiled from "DVShapeFactory.scala"
public final class org.nees.rpi.vis.PressureSensor extends java.lang.Object{
    public static final java.lang.Object productElement(int);
    public static final int productArity();
    public static final java.lang.String productPrefix();
    public static final int $tag();
    public static final java.lang.String displayName();
}

Compiled from "DVShapeFactory.scala"
public final class org.nees.rpi.vis.PressureSensor$ extends org.nees.rpi.vis.PresetShapeType implements scala.ScalaObject,scala.Product,java.io.Serializable{
    public static final org.nees.rpi.vis.PressureSensor$ MODULE$;
    public static {};
    public org.nees.rpi.vis.PressureSensor$();
    public java.lang.Object readResolve();
    public java.lang.Object productElement(int);
    public int productArity();
    public java.lang.String productPrefix();
    public final java.lang.String toString();
    public int $tag();
}

但这并没有产生任何深刻的见解。

I created a hierarchy of case objects in Scala that looks like the following:

package my.awesome.package

sealed abstract class PresetShapeType(val displayName: String)

case object AccelerationSensor extends PresetShapeType("Acceleration Sensor")
case object DisplacementSensor extends PresetShapeType("Displacement Sensor")
case object ForceSensor        extends PresetShapeType("Force Sensor")
case object PressureSensor     extends PresetShapeType("Pressure Sensor")
case object StrainSensor       extends PresetShapeType("Strain Sensor")

I also have a piece of Java code in which I'd like to access PressureSensor, but the following does not work:

package my.awesome.package.subpackage;

import my.awesome.package.PressureSensor;

// Do some stuff, then...

DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor, new Point3f(0,0,0));

So, how do I reference the PressureSensor case object from Java? I decompiled the byte code for both the PressureSensor and PressureSensor$ classes, which yielded the following:

Compiled from "DVShapeFactory.scala"
public final class org.nees.rpi.vis.PressureSensor extends java.lang.Object{
    public static final java.lang.Object productElement(int);
    public static final int productArity();
    public static final java.lang.String productPrefix();
    public static final int $tag();
    public static final java.lang.String displayName();
}

Compiled from "DVShapeFactory.scala"
public final class org.nees.rpi.vis.PressureSensor$ extends org.nees.rpi.vis.PresetShapeType implements scala.ScalaObject,scala.Product,java.io.Serializable{
    public static final org.nees.rpi.vis.PressureSensor$ MODULE$;
    public static {};
    public org.nees.rpi.vis.PressureSensor$();
    public java.lang.Object readResolve();
    public java.lang.Object productElement(int);
    public int productArity();
    public java.lang.String productPrefix();
    public final java.lang.String toString();
    public int $tag();
}

But that didn't yield any great insight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对不⑦ 2024-09-03 08:51:22

从 Java 说:

my.awesome.package.PressureSensor$.MODULE$

from Java, say:

my.awesome.package.PressureSensor$.MODULE$
情话难免假 2024-09-03 08:51:22

PressureSensor$.MODULE$ 应该为您提供案例对象的实例。

PressureSensor$.MODULE$ should give you the instance of the case object.

温柔嚣张 2024-09-03 08:51:22

这仍然是一个 hack,但我认为在 Java 中更具可读性。只需添加一个方法来显式返回对单例实例的引用(它在类上显示为静态方法):

sealed abstract class PresetShapeType(val displayName: String)

case object AccelerationSensor extends PresetShapeType("Acceleration Sensor") { def instance = this }
case object DisplacementSensor extends PresetShapeType("Displacement Sensor") { def instance = this }
case object ForceSensor extends PresetShapeType("Force Sensor") { def instance = this }
case object PressureSensor extends PresetShapeType("Pressure Sensor") { def instance = this }
case object StrainSensor extends PresetShapeType("Strain Sensor") { def instance = this }

然后在 Java 中:

import my.awesome.package.PressureSensor;
DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor.instance(), new Point3f(0,0,0));

This is still a hack, but in my opinion a bit more readable in Java. Just add a method to explicitly return the reference to the singleton instance (it shows up as a static method on the class):

sealed abstract class PresetShapeType(val displayName: String)

case object AccelerationSensor extends PresetShapeType("Acceleration Sensor") { def instance = this }
case object DisplacementSensor extends PresetShapeType("Displacement Sensor") { def instance = this }
case object ForceSensor extends PresetShapeType("Force Sensor") { def instance = this }
case object PressureSensor extends PresetShapeType("Pressure Sensor") { def instance = this }
case object StrainSensor extends PresetShapeType("Strain Sensor") { def instance = this }

And then in Java:

import my.awesome.package.PressureSensor;
DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor.instance(), new Point3f(0,0,0));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文