ラムダ式が導入されるということで、さっそく試してみました。
ラムダ式とは関数を抽象化したもので、コンパクトにコールバック関数の引渡し・定義ができます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
public class LambdaListener extends Frame | |
{ | |
Button button=new Button("OK"); | |
LambdaListener() | |
{ | |
add(button); | |
button.addActionListener(e->System.out.println("OK button pressed.")); | |
setSize(100,100); | |
setTitle("Lambda"); | |
setVisible(true); | |
} | |
public static void main(String args[]) | |
{ | |
LambdaListener test=new LambdaListener(); | |
} | |
} |
(引数) -> 定義
となります。Lisp などとは異なる形式ですが、内部クラスなどと比較して簡潔にコールバック関数が定義できます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LambdaMethod | |
{ | |
public static void main(String args[]) | |
{ | |
Test test=(name)->System.out.println("Hello "+name); | |
test.say("name"); | |
} | |
private interface Test | |
{ | |
public void say(String name); | |
} | |
} |