请参阅https://www.scala-lang.org/files/archive/spec/2.11/02-identifiers-names-and-scopes.html的示例,我注意到在示例中package P
被定义了两次。这个例子不正确吗?我试图在Scastie中运行它,但是代码无法编译。
https://scastie.scala-lang.org/ZZ3vjsNzSvWnY1gFCqaNAg
package P {
object X { val x = 1; val y = 2 }
}
package Q {
object X { val x = true; val y = "" }
}
// The following program illustrates different kinds of bindings and precedences between them.
//isn't this redefining P??
package P { // `X' bound by package clause
import Console._ // `println' bound by wildcard import
object A {
println("L4: "+X) // `X' refers to `P.X' here
object B {
import Q._ // `X' bound by wildcard import
println("L7: "+X) // `X' refers to `Q.X' here
import X._ // `x' and `y' bound by wildcard import
println("L8: "+x) // `x' refers to `Q.X.x' here
object C {
val x = 3 // `x' bound by local definition
println("L12: "+x) // `x' refers to constant `3' here
{ import Q.X._ // `x' and `y' bound by wildcard import
// println("L14: "+x) // reference to `x' is ambiguous here
import X.y // `y' bound by explicit import
println("L16: "+y) // `y' refers to `Q.X.y' here
{ val x = "abc" // `x' bound by local definition
import P.X._ // `x' and `y' bound by wildcard import
// println("L19: "+y) // reference to `y' is ambiguous here
println("L20: "+x) // `x' refers to string "abc" here
}}}}}}
答案 0 :(得分:0)
例如,它不是重新定义,而是添加到软件包package P {
object X { val s = "hello"}
}
package P {
object Y { val s = "world"}
}
object Hello extends App {
println(P.X.s)
println(P.Y.s)
}
hello
world
输出
X
这意味着Y
和P
现在可以从软件包requests
中获得