事件 – Scala Swing事件中使用的反引号

栏目: Scala · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/6564220/backquote-used-in-scala-swing-event
我是 Scala

的新手,并按照其中一个例子让Swing在Scala中工作,我有一个问题.基于,

listenTo(celsius, fahrenheit)
   reactions += {
      case EditDone(`fahrenheit`) =>
        val f = Integer.parseInt(fahrenheit.text)
        celsius.text = ((f - 32) * 5 / 9).toString

      case EditDone(`celsius`) =>
        val c = Integer.parseInt(celsius.text)
        fahrenheit.text = ((c * 9) / 5 + 32).toString
    }

为什么我必须在EditDone(`fahrenheit`)和EditDone(`celsius`)中使用反引号(`)来识别我的文本域组件,例如华氏温度和摄氏温度?为什么我不能只使用EditDone(fahrenheit)呢?

谢谢

这与模式匹配有关.如果在模式匹配中使用小写名称:

reactions += {
  case EditDone(fahrenheit) => // ...
}

然后匹配的对象(在这种情况下为事件)将与任何小部件上的任何EditDone事件匹配.它会将对窗口小部件的引用绑定到名称fahrenheit.华氏温度成为该案例范围内的新价值.

但是,如果你使用反引号:

val fahrenheit = new TextField
...
reactions += {
  case EditDone(`fahrenheit`) => // ...
}

然后,只有当EditDone事件引用之前定义的值fahrenheit引用的现有对象时,模式匹配才会成功.

请注意,如果值华氏度的名称是大写的,就像华氏度,那么你就不必使用反引号 – 就好像你已经把它们放了一样.如果要在范围中包含要匹配的常量或对象,这将非常有用 – 这些常量或对象通常具有大写名称.

翻译自:https://stackoverflow.com/questions/6564220/backquote-used-in-scala-swing-event


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Domain-Driven Design

Domain-Driven Design

Eric Evans / Addison-Wesley Professional / 2003-8-30 / USD 74.99

"Eric Evans has written a fantastic book on how you can make the design of your software match your mental model of the problem domain you are addressing. "His book is very compatible with XP. It is n......一起来看看 《Domain-Driven Design》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具