Scala使用最终的静态变量
发布时间:2020-12-16 18:25:51 所属栏目:安全 来源:网络整理
导读: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"} 基本上,我导入Foo后如何使用Bar .Bar,我是否真的每次都要调用Foo.Bar?
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" } 基本上,我导入Foo后如何使用Bar .Bar,我是否真的每次都要调用Foo.Bar? 解决方法
辅助构造函数具有外部作用域,以防止您像这样做一些愚蠢的事情:
class Silly(foo: String) { val bar = 123 def this() = this(bar.toString) } 在构造函数中创建参数后,尝试将参数传递给构造函数的位置. 不幸的是,这意味着导入Foo.Bar不在该行的范围内.你必须使用完整路径Foo.Bar. 对于除了其他构造函数之外的类中的所有内容,Foo.Bar将作为Bar的范围. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |