「Java8」ラムダ式(lambda expression)には複数引数のサンプル

2020年11月9日

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.cft;
//interface definition
public interface InterFaceA {
//method definition
int testFunc(int x, int b);
}
package com.arkgame.study.cft; //interface definition public interface InterFaceA { //method definition int testFunc(int x, int b); }
package com.arkgame.study.cft;

//interface definition
public interface InterFaceA {
      //method definition
      int testFunc(int x, int b);
}

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.cft;
public class InterfaceRunDemo {
public static void main(String[] args) {
//method を呼び出す
interMethod();
}
public static void interMethod() {
// {}なし
InterFaceA interA = (a, b) -> {
return a + b;
};
int result1 = interA.testFunc(15, 25);
System.out.println("加算結果1: " + result1);
// {} あり
InterFaceA interB = (a, b) -> {
return a + b;
};
int result2 = interB.testFunc(7, 8);
System.out.println("加算結果2: " + result2);
}
}
package com.arkgame.study.cft; public class InterfaceRunDemo { public static void main(String[] args) { //method を呼び出す interMethod(); } public static void interMethod() { // {}なし InterFaceA interA = (a, b) -> { return a + b; }; int result1 = interA.testFunc(15, 25); System.out.println("加算結果1: " + result1); // {} あり InterFaceA interB = (a, b) -> { return a + b; }; int result2 = interB.testFunc(7, 8); System.out.println("加算結果2: " + result2); } }
package com.arkgame.study.cft;

public class InterfaceRunDemo {

      public static void main(String[] args) {
            //method を呼び出す
            interMethod();
      }

      public static void interMethod() {
            // {}なし
            InterFaceA interA = (a, b) -> {
                  return a + b;
            };
            int result1 = interA.testFunc(15, 25);
            System.out.println("加算結果1: " + result1);

            // {} あり
            InterFaceA interB = (a, b) -> {
                  return a + b;
            };
            int result2 = interB.testFunc(7, 8);
            System.out.println("加算結果2: " + result2);

      }
}

3.実行結果
加算結果1: 40
加算結果2: 15

Java

Posted by arkgame