Advertisement
Guest User

Function Pointer Demo

a guest
Feb 13th, 2012
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1.  
  2. public class FunctionPointerDemo {
  3.  
  4.         public static void main(String[] args) {
  5.  
  6.                 // bigger than implementation
  7.                 FunctionPointer fp = new FunctionPointer() {
  8.                         public String function(int a, int b) {
  9.                                 if (a > b)
  10.                                         return "bigger";
  11.                                 else
  12.                                         return "not bigger";
  13.                         }
  14.                 };
  15.                 comparison(1, 2, fp);
  16.                 comparison(3, 2, fp);
  17.  
  18.                 // smaller than implementation
  19.                 FunctionPointer fp2 = new FunctionPointer() {
  20.                         public String function(int a, int b) {
  21.                                 if (a < b)
  22.                                         return "smaller";
  23.                                 else
  24.                                         return "not smaller";
  25.                         }
  26.                 };
  27.                 comparison(1, 2, fp2);
  28.                 comparison(3, 2, fp2);
  29.         }
  30.  
  31.         // prototype
  32.         interface FunctionPointer {
  33.                 public String function(int a, int b);
  34.         }
  35.  
  36.         // function with fp class as argument
  37.         public static void comparison(int a, int b, FunctionPointer fp) {
  38.                 System.out.printf("%d is %12s than %d\n", a, fp.function(a, b), b);
  39.         }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement