nob*_*ody 4 syntax static scala
class Foo(bar: String) {
import Foo.Bar
def this() = this(Bar) // this line fails, it seems I can only do
// def this() = this(Foo.Bar)
}
object Foo {
val Bar = "Hello Bar"
}
Run Code Online (Sandbox Code Playgroud)
基本上,我怎么用Bar后import Foo.Bar,我真的Foo.Bar每次都要打电话吗?
Rex*_*err 13
辅助构造函数具有外部作用域,以防止您像这样做一些愚蠢的事情:
class Silly(foo: String) {
val bar = 123
def this() = this(bar.toString)
}
Run Code Online (Sandbox Code Playgroud)
在构造函数中创建参数后,尝试将参数传递给构造函数的位置.
不幸的是,这意味着import Foo.Bar该行不在范围内.你必须使用完整的路径Foo.Bar.
对于除了其他构造函数之外的类中的所有内容,Foo.Bar将在范围内Bar.
如果你只是在类定义之外导入怎么办?
import Foo.Bar
class Foo(bar: String) {
def this() = this(Bar)
}
object Foo {
val Bar = "Hello Bar"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
749 次 |
| 最近记录: |