如何从 Java“获取”Scala case 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2561415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:02:10  来源:igfitidea点击:

How do I "get" a Scala case object from Java?

scalacase-class

提问by mipadi

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

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

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:

我还有一段 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));

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

那么,如何PressureSensor从 Java引用case 对象呢?我反编译了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();
}

But that didn't yield any great insight.

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

回答by Seth Tisue

from Java, say:

来自 Java,说:

my.awesome.package.PressureSensor$.MODULE$

回答by Geoff Reedy

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

PressureSensor$.MODULE$应该给你 case 对象的实例。

回答by metasim

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):

这仍然是一个 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 }

And then in Java:

然后在 Java 中:

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