24th: A walkthrough of Sam conversion in Scala

Source: Internet
Author: User

SAM is the abbreviation for single abstract method, which is that some interfaces have only one abstract approach


Let's say we need to make a button that records the number of clicks in the background whenever the button is pressed. In Java, the code is implemented in the following way:

Package com.dt.scala.funcimport javax.swing.jbuttonimport java.awt.event.actionlistenerimport  java.awt.event.actioneventimport javax.swing.jframeobject sam {  def main ( Args: array[string]): unit = {    var data = 0     val frame = new jframe ("sam testing");   val jButton  = new jbutton ("Counter")   jbutton.addactionlistener (new actionlistener {     override def actionperformed (event: actionevent)  {       DATA += 1      PRINTLN (data)      }  })        frame.setcontentpane (JButton);        frame.pack ();       frame.setvisible (True);     }}

Jbuttton.addactionlistener in the code, we only care about

Data + = 1PRINTLN (data)

The other is the boilerplate code, each time to repeat the write. In Scala, we can use implicit conversions to simplify the notation. As follows

Package com.dt.scala.funcimport javax.swing.jbuttonimport java.awt.event.actionlistenerimport  java.awt.event.actioneventimport javax.swing.jframeobject sam {  def main ( Args: array[string]): unit = {    var data = 0     val frame = new jframe ("sam testing");     val  jbutton = new jbutton ("Counter")     implicit def  Convertedaction (action:  (actionevent)  => unit)  =         new actionlistener {  override def actionperformed (event:  ActionEvent)  { action (event)  }    }         jbutton.addactionlistener (event: actionevent)  => {data += 1;  println (data)})     fraMe.setcontentpane (JButton);        frame.pack ();       frame.setvisible (true);     }}


Our functional code is simplified to

Jbutton.addactionlistener ((event:actionevent) = {data + = 1; println (data)})


This feature is called Sam conversion.

This article is from the "Ding Dong" blog, please be sure to keep this source http://lqding.blog.51cto.com/9123978/1741886

24th: A walkthrough of Sam conversion in Scala

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.