How do I “get” a Scala case object from Java?

▼魔方 西西 提交于 2019-11-28 22:32:30
Seth Tisue

from Java, say:

my.awesome.package.PressureSensor$.MODULE$

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

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));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!