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"
}基本上,我在import Foo.Bar之后如何使用Bar,我真的每次都要调用Foo.Bar吗?
发布于 2012-03-22 16:36:46
辅助构造函数有外部作用域,可以防止你做这样的傻事:
class Silly(foo: String) {
val bar = 123
def this() = this(bar.toString)
}您尝试将一个参数传递给在构造函数中创建它的constructor...after。
不幸的是,这意味着import Foo.Bar不在该行的范围内。您必须使用完整路径Foo.Bar。
对于类中除附加构造函数之外的所有内容,Foo.Bar都将在作用域中作为Bar。
https://stackoverflow.com/questions/9818570
复制相似问题