如何在使用别名引用外部 class "this" 引用时创建内部 class 实例
How to create an inner class instance while using alias name for referring the outer class "this" reference
我在尝试创建内部 [=43= 的实例时遇到编译错误 “无法解析符号 #INNER_CLASS_NAME” ].
请注意,只有当我创建了别名来引用这个包含外部 class.
的引用时,我才会收到此错误
请参考下面的代码片段了解更多详情:
package netsting
object nested_class4 extends App {
class Network(val name: String) {
class Member(val name: String) {
def description = s"Inner class field name :${Network.this.name}, outer name : ${this.name} "
}
}
class Network2(val name: String) { outer => {
class Member2(val name: String) {
def description = s"Inner class field name :${outer.name}, outer name : ${this.name} "
}
}
}
val net1 = new Network("net1")
val mem1 = new net1.Member("mem1")
// TODO : How to create instance of inner class while using alias name reference for enclosing class instance
val net2 = new Network2("net1")
val mem2 = new net2.Member2("mem1")
println(mem2.description)
}
在这里,在上面的例子中,我可以创建内部 class Network#Member 的实例而不会出现任何错误。但是,我在尝试创建内部 class Network2#Member2.
实例时遇到编译错误
在这里,如果我删除别名“outer”并使用随意方式Network.this.name[=35,编译错误就会消失=] 来引用包含 class 个字段。
当我们创建了别名来引用外部 class 的 this 引用时,创建内部 class 实例的正确方法是什么?
“别名”的语法不包含任何额外的大括号:
class Network2(val name: String) { outer => // MUST be like this
class Member2(val name: String) {
def description = s"Inner class field name: ${outer.name}, outer name: ${this.name}"
}
}
// WRONG
class Network2Bad(val name: String) { outer => { ??? } }
// same as
// class Network2Bad(val name: String) { { ??? } }
// the body of this class consists of a block expression { ??? }
// which is evaluated whenever a Network2Bad is constructed
// by ordinary scoping rules, nothing declared in that block is declared in the enclosing scope (the Network2Bad object)
val net2 = new Network2("net2")
val mem2 = new net2.Member2("mem2")
println(mem2.description)
P.S。我所知道的唯一官方名称是“别名”。
我在尝试创建内部 [=43= 的实例时遇到编译错误 “无法解析符号 #INNER_CLASS_NAME” ].
请注意,只有当我创建了别名来引用这个包含外部 class.
的引用时,我才会收到此错误请参考下面的代码片段了解更多详情:
package netsting
object nested_class4 extends App {
class Network(val name: String) {
class Member(val name: String) {
def description = s"Inner class field name :${Network.this.name}, outer name : ${this.name} "
}
}
class Network2(val name: String) { outer => {
class Member2(val name: String) {
def description = s"Inner class field name :${outer.name}, outer name : ${this.name} "
}
}
}
val net1 = new Network("net1")
val mem1 = new net1.Member("mem1")
// TODO : How to create instance of inner class while using alias name reference for enclosing class instance
val net2 = new Network2("net1")
val mem2 = new net2.Member2("mem1")
println(mem2.description)
}
在这里,在上面的例子中,我可以创建内部 class Network#Member 的实例而不会出现任何错误。但是,我在尝试创建内部 class Network2#Member2.
实例时遇到编译错误在这里,如果我删除别名“outer”并使用随意方式Network.this.name[=35,编译错误就会消失=] 来引用包含 class 个字段。
当我们创建了别名来引用外部 class 的 this 引用时,创建内部 class 实例的正确方法是什么?
“别名”的语法不包含任何额外的大括号:
class Network2(val name: String) { outer => // MUST be like this
class Member2(val name: String) {
def description = s"Inner class field name: ${outer.name}, outer name: ${this.name}"
}
}
// WRONG
class Network2Bad(val name: String) { outer => { ??? } }
// same as
// class Network2Bad(val name: String) { { ??? } }
// the body of this class consists of a block expression { ??? }
// which is evaluated whenever a Network2Bad is constructed
// by ordinary scoping rules, nothing declared in that block is declared in the enclosing scope (the Network2Bad object)
val net2 = new Network2("net2")
val mem2 = new net2.Member2("mem2")
println(mem2.description)
P.S。我所知道的唯一官方名称是“别名”。