public class FunctionPointerDemo {
public static void main(String[] args) {
// bigger than implementation
FunctionPointer fp = new FunctionPointer() {
public String function(int a, int b) {
if (a > b)
return "bigger";
else
return "not bigger";
}
};
comparison(1, 2, fp);
comparison(3, 2, fp);
// smaller than implementation
FunctionPointer fp2 = new FunctionPointer() {
public String function(int a, int b) {
if (a < b)
return "smaller";
else
return "not smaller";
}
};
comparison(1, 2, fp2);
comparison(3, 2, fp2);
}
// prototype
interface FunctionPointer {
public String function(int a, int b);
}
// function with fp class as argument
public static void comparison(int a, int b, FunctionPointer fp) {
System.out.printf("%d is %12s than %d\n", a, fp.function(a, b), b);
}
}