Skip to content

Instantly share code, notes, and snippets.

@Gab-km
Created October 20, 2014 13:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gab-km/bf48d0a6e761d37b0d0f to your computer and use it in GitHub Desktop.
Save Gab-km/bf48d0a6e761d37b0d0f to your computer and use it in GitHub Desktop.
高階関数とインターフェイス
class Adder implements Applyable {
@override
public int func(int a, int b) {
return a + b;
}
}
interface Applyable {
public abstract int func(int a, int b);
}
class Multiplier implements Applyable {
@override
public int func(int a, int b) {
return a * b;
}
}
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