Created
October 20, 2014 13:47
-
-
Save Gab-km/bf48d0a6e761d37b0d0f to your computer and use it in GitHub Desktop.
高階関数とインターフェイス
This file contains 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
class Adder implements Applyable { | |
@override | |
public int func(int a, int b) { | |
return a + b; | |
} | |
} |
This file contains 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
interface Applyable { | |
public abstract int func(int a, int b); | |
} |
This file contains 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
class Multiplier implements Applyable { | |
@override | |
public int func(int a, int b) { | |
return a * b; | |
} | |
} |
This file contains 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
class Program { | |
public static void main(string[] args) { | |
Applyable adder = new Adder(); | |
System.out.println(apply_2_3(adder)); //=> 5 | |
Applyable multiplier = new Multiplier(); | |
System.out.println(apply_2_3(multiplier)); //=> 6 | |
} | |
private static int apply_2_3(Applyable ap) { | |
return ap.func(2, 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment