【发布时间】:2016-06-08 09:36:23
【问题描述】:
从具有公共成员的 Java 类继承时,使用 scala 的 override 修饰符会引发编译错误,如下所示:
Error:(67, 22) overriding variable userId in class Logon of type String;
value userId has incompatible type
override val userId = s.user
^
java 类看起来大致如下:
public class Logon {
public String userId = "";
}
和scala代码:
class MyLogon extends Logon {
override val userId : String = "abc"
}
删除修饰符会导致:
Error:(72, 7) overriding variable userId in class Logon of type String;
value userId needs `override' modifier
val userId: String = s.user
^
为什么会这样?它是一个错误吗?有相关的问题,例如16607517 但这些似乎正在改变字段的可见性;这不是这里的情况 - 它们都是公开的。
相信这是 scalac-2.10.4。
【问题讨论】:
标签: scala inheritance