Scala-Java 不兼容引用与静态内部类同名的类中的静态字段

发布于 2024-11-04 01:57:12 字数 732 浏览 1 评论 0原文

拿这个Java类来说:

public class Fisk {

    public static class A {
    }

    public static A A = new A();
}

这个Java代码可以工作:

    Fisk.A a = new Fisk.A();
    Fisk.A b = Fisk.A;

但是从Scala调用它:

    val fisk = new Fisk.A()
    val strupp = Fisk.A

会导致编译器错误:

error: ambiguous reference to overloaded definition,
[INFO] both variable A in object Fisk of type Fisk.A
[INFO] and  object A in object Fisk of type object Fisk.A
[INFO] match expected type ?
[INFO]          val strupp = Fisk.A
[INFO]                                   ^
[ERROR] one error found

任何人都知道解决这个问题的方法,还是我必须重命名我的静态字段?

-- 安德烈亚斯

Take this Java class:

public class Fisk {

    public static class A {
    }

    public static A A = new A();
}

This Java code works:

    Fisk.A a = new Fisk.A();
    Fisk.A b = Fisk.A;

But calling it from Scala:

    val fisk = new Fisk.A()
    val strupp = Fisk.A

results in compiler-error:

error: ambiguous reference to overloaded definition,
[INFO] both variable A in object Fisk of type Fisk.A
[INFO] and  object A in object Fisk of type object Fisk.A
[INFO] match expected type ?
[INFO]          val strupp = Fisk.A
[INFO]                                   ^
[ERROR] one error found

Anyone knows a way around this, or do I have to rename my static field?

--
Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-11-11 01:57:12
scala> Fisk.A
<console>:8: error: ambiguous reference to overloaded definition,
both variable A in object Fisk of type Fisk.A
and  object A in object Fisk of type object Fisk.A
match expected type ?
       Fisk.A
            ^
// this is the static field A of Fisk
scala> Fisk.A: Fisk.A
res1: Fisk.A = Fisk$A@d86c58

// this is a new constructed instance of type Fisk.A
scala> val fisk = new Fisk.A()
fisk: Fisk.A = Fisk$A@462f90

// this is the static field A of Fisk (see the same hashcode)
scala> val strupp: Fisk.A = Fisk.A
strupp: Fisk.A = Fisk$A@d86c58
scala> Fisk.A
<console>:8: error: ambiguous reference to overloaded definition,
both variable A in object Fisk of type Fisk.A
and  object A in object Fisk of type object Fisk.A
match expected type ?
       Fisk.A
            ^
// this is the static field A of Fisk
scala> Fisk.A: Fisk.A
res1: Fisk.A = Fisk$A@d86c58

// this is a new constructed instance of type Fisk.A
scala> val fisk = new Fisk.A()
fisk: Fisk.A = Fisk$A@462f90

// this is the static field A of Fisk (see the same hashcode)
scala> val strupp: Fisk.A = Fisk.A
strupp: Fisk.A = Fisk$A@d86c58
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文