4

I am using a java library in which a class has a member named "type". If I do something like this:

class MyClass{
  public MyClass(){
    type = 5;
  }
  public int type;
}

then the java compiler compiles it fine. But if I try to access it from scala:

val x = new MyClass()
x.type = 10

I get this message:

identifier expected but 'type' found.

How do I work around this issue?

I am guessing this has come up before but I could not find a related question.

4

1 回答 1

7

您可以使用反引号在 scala 中执行此操作:

x.`type` = 10

任何包含在反引号中的字符串都可以用作标识符,或访问标识符。

于 2013-08-27T04:51:48.867 回答