加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 无法覆盖java.lang.String字段.怎么了?

发布时间:2020-12-16 19:25:14 所属栏目:安全 来源:网络整理
导读:我试图编译包含的代码 class FixedIndexedRepository(override val name: java.lang.String,location: URI) extends FixedIndexedRepo 其中扩展了FixedIndexedRepo,它扩展了Java类AbstractIndexedRepo public abstract class AbstractIndexedRepo implements
我试图编译包含的代码

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

其中扩展了FixedIndexedRepo,它扩展了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

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

解决方法

雷克斯说这很难看:

Making a public accessor from an inherited protected Java field

鉴于:

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}"
}

你被预先警告了丑陋.

我也没有详细说明细节,但周末即将到来.

Unfortunately Scala 2.9.2 compiler stops with an error

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读