scala入门初级代码训练-23sam隐式转换

原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-23/

There I am,in the world more exciting!

by postbird

www.ptbird.cn


package com.ptbird.scala

import java.awt.event.{ActionEvent, ActionListener}
import javax.swing.{JButton, JFrame}

/**
* Created by postbird on 2016/5/26.
*/
object SAMTest {
def main(args: Array[String]): Unit = {

var data = 0;
val frame = new JFrame("SAM test")
val jbutton = new JButton("SAM Test")
// jbutton.addActionListener(new ActionListener {
// override def actionPerformed(e: ActionEvent): Unit = {
// data += 1
// println(data)
// }
// })

/** **********************************隐式转换 通过高级函数传递参数 *******************************************/

implicit def convertedAction(action:(ActionEvent)=>Unit)={
new ActionListener {
override def actionPerformed(event: ActionEvent): Unit = {
action(event)
}
}
}
jbutton.addActionListener((event:ActionEvent)=>{data+=1;println(data)})

frame.setContentPane(jbutton)
frame.pack()
frame.setVisible(true)
}
}

欢迎评论。
lingkb » scala入门初级代码训练-23sam隐式转换

发表评论