「Java8」ラムダ式(lambda expression)にはメソッドの引数を渡すサンプル

構文
インターフェース名 オブジェクト名 =(引数1,引数2…)->{
//some code
}
使用例
1.インターフェースの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.cft;
//インターフェース
public interface EmpInfo {
//メソッド string return value
public String func(int age,String str);
}
package com.arkgame.study.cft; //インターフェース public interface EmpInfo { //メソッド string return value public String func(int age,String str); }
package com.arkgame.study.cft;

//インターフェース
public interface EmpInfo {
      //メソッド string return value
      public String  func(int age,String str);

}

2.インターフェースの実装

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.cft;
public class InterFaceDemo {
public static void main(String[] args) {
interMethod();
}
public static void interMethod() {
EmpInfo emp = (age, res) -> {
age = 11;
res = "sample return value";
return res;
};
System.out.println("引数あるメソッドの戻り値を返す: " + emp.func(22, "abcde"));
}
}
package com.arkgame.study.cft; public class InterFaceDemo { public static void main(String[] args) { interMethod(); } public static void interMethod() { EmpInfo emp = (age, res) -> { age = 11; res = "sample return value"; return res; }; System.out.println("引数あるメソッドの戻り値を返す: " + emp.func(22, "abcde")); } }
package com.arkgame.study.cft;

public class InterFaceDemo {

      public static void main(String[] args) {
            interMethod();
      }

      public static void interMethod() {

            EmpInfo emp = (age, res) -> {
                  age = 11;
                  res = "sample return value";
                  return res;
            };
            System.out.println("引数あるメソッドの戻り値を返す: " + emp.func(22, "abcde"));
      }
}

3.実行結果
引数あるメソッドの戻り値を返す: sample return value

Java

Posted by arkgame