【问题标题】:Why scala-swing Button can be assigned to MainFrame's contents?为什么 scala-swing Button 可以分配给 MainFrame 的内容?
【发布时间】:2020-01-26 06:08:51
【问题描述】:

在 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。那我为什么要写

top.contents = new Button("a")

没有错误?

【问题讨论】:

    标签: scala scala-swing


    【解决方案1】:

    根据 API docs注意以下两个方法签名

    def contents: Seq[Component]
    def contents_=(c: Component): Unit
    

    赋值语法

    top.contents = new Button("a")
    

    其实是利用了mutator_=方法

    def contents_=(c: Component): Unit
    

    正如Accessors/Mutators所解释的那样

    对于 mutators,方法的名称应该是 附加“_=”的属性。只要有相应的访问器 该特定属性名称是在封闭类型上定义的,这 约定将启用镜像的调用点突变语法 任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多