2

次の 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

誰かがこれを回避する方法を知っていますか、それとも静的フィールドの名前を変更する必要がありますか?

-- アンドレアス

4

2 に答える 2

1
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
于 2011-04-29T09:12:47.487 に答える
0

ここに解決策とともに説明があります

于 2011-05-06T11:48:14.280 に答える