4

我试图编译包含

class FixedIndexedRepository(override val name: java.lang.String, location: URI) extends FixedIndexedRepo

which extends FixedIndexedRepowhich extends Java 类AbstractIndexedRepo

public abstract class AbstractIndexedRepo implements RegistryPlugin, Plugin, RemoteRepositoryPlugin, IndexProvider, Repository {
...
protected String name = this.getClass().getName();
...

不幸的是,Scala 2.9.2 编译器因错误而停止:

.../FixedIndexedRepository.scala:29: overriding variable name in class AbstractIndexedRepo of type java.lang.String;
[error]  value name has incompatible type
[error] class FixedIndexedRepository(override val name: java.lang.String, location: URI) extends FixedIndexedRepo

如何解决这个问题?怎么了?

4

1 回答 1

1

雷克斯说它很丑:

从继承的受保护的 Java 字段创建公共访问器

鉴于:

package j;

public class HasName {
    protected String name = "name";
}

那么假出是:

package user

private[user] class HasNameAdapter extends j.HasName {
  protected def named: String = name
  protected def named_=(s: String) { name = s }
}

class User(n: String = "nom") extends HasNameAdapter {
  def name(): String = named
  def name_=(s: String) { this named_= s }
  this name_= n
}

object Test extends App {
  val u = new User("bob")
  Console println s"user ${u.name()}"
  Console println s"user ${u.name}"
}

你被预先警告过丑陋的。

我也没有完全弄清楚细节,但是周末快到了。

不幸的是,Scala 2.9.2 编译器因错误而停止

你的意思是,幸运的是它会因错误而停止。

于 2013-05-17T22:02:59.743 回答