【问题标题】:How to use a backquoted scala identifier in java?如何在java中使用反引号的scala标识符?
【发布时间】:2014-05-06 12:50:42
【问题描述】:

我正在开发一个分析 Java 代码的 Java/Scala 项目。在我的 scala 代码中,我有:

package p{
   object NodeKind{
      // In real code they are not Int but this is good enough for the example
      val `class` = 0 
      val interface = 0
   }
}

我需要从 Java 访问它

class MyJavaClass{
     void m(){
        /* ... */
        int value = p.NodeKind.//how can I access to `class` or interface ?
        /* ... */
     }
}

现在我已经解决了 Brian Agnew 解决方案,我添加了一个字段

  val classKind = `class`

在我的 scala 对象中,我可以在我的 java 代码中使用 p.nodeKind.classKind() 访问它,但我真的更想知道 `class` 的编译(修改?)规则和直接访问它的接口。

【问题讨论】:

标签: java scala interop


【解决方案1】:

我怀疑你的两个选择可能是:

  1. 在 Scala 类中创建访问器方法
  2. 使用反射在 Java 类中创建访问器方法以查找并读取该字段

例如

class A {
   val class = 1
   def clazz = class
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2012-04-21
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2015-08-10
    相关资源
    最近更新 更多