为什么 scala-swing Button 可以赋值给 MainFrame 的内容?
Why scala-swing Button can be assigned to MainFrame's contents?
在scala-swing中,我可以写出这样简单的代码:
import scala.swing._
object HelloWorld2 extends SimpleSwingApplication {
val top = new MainFrame()
top.title = "Hello, World!"
top.contents = new Button("a")
}
它工作正常,但根据 doc MainFrame
中 contents
的类型是 Seq[Component]
而 Button 的类型是 Button
。那为什么我可以写
top.contents = new Button("a")
没有错误?
请注意以下两个方法签名 API docs
def contents: Seq[Component]
def contents_=(c: Component): Unit
赋值语法
top.contents = new Button("a")
实际上使用了 mutator _=
方法
def contents_=(c: Component): Unit
所述
For mutators, the name of the method should be the name of the
property with “_=” appended. As long as a corresponding accessor with
that particular property name is defined on the enclosing type, this
convention will enable a call-site mutation syntax which mirrors
assignment.
在scala-swing中,我可以写出这样简单的代码:
import scala.swing._
object HelloWorld2 extends SimpleSwingApplication {
val top = new MainFrame()
top.title = "Hello, World!"
top.contents = new Button("a")
}
它工作正常,但根据 doc MainFrame
中 contents
的类型是 Seq[Component]
而 Button 的类型是 Button
。那为什么我可以写
top.contents = new Button("a")
没有错误?
请注意以下两个方法签名 API docs
def contents: Seq[Component]
def contents_=(c: Component): Unit
赋值语法
top.contents = new Button("a")
实际上使用了 mutator _=
方法
def contents_=(c: Component): Unit
所述
For mutators, the name of the method should be the name of the property with “_=” appended. As long as a corresponding accessor with that particular property name is defined on the enclosing type, this convention will enable a call-site mutation syntax which mirrors assignment.