How to implement the Java equivalent of function pointers through abstract classes

I am trying to create a simple four-function calculator using a jump table without a switch or if / else statement. I understand that I can create a transition table using function pointers, but I'm kind. I started by adding and subtracting parts of the program, but I'm trying to understand the design / strategy to use. I also get an error when putting the method into an array, so far this is what I have:

public class Calculator {

          public abstract class Functor{

            abstract double Compute(double op1, double op2);
            }

     class Addition extends Functor
    {

        public double Compute(double op1, double op2){ return op1 + op2;}
    }

    class Subtraction extends Functor
    {

        public double Compute(double op1, double op2){ return op1 - op2;}
    }



    public static void main(String[] args) {

        Functor add = new Addition();     // Problem here
        Functor sub = new Subtraction();  // and here



    }
}

Any help or ideas for stepping in the right direction is greatly appreciated! thanks in advance!

+3
source share
3 answers

( , , ), -, .

abstract class Functor {


    public abstract double compute(double a, double b);

    public static void main(String[] args) {
        Functor add = new Functor() {
            // defining it here is essentially how you do a function pointer
            public double compute(double a, double b) { return a + b; }
        };

        Functor subtract = new Functor() {
            public double compute(double a, double b) { return a - b; }
        };

        System.out.println(add.compute(1.0,2.0));
        System.out.println(subtract.compute(1.0,2.0));

    }

}

:

C:\Documents and Settings\glowcoder\My Documents>java Functor
3.0
-1.0

C:\Documents and Settings\glowcoder\My Documents>
+2

:

public enum Operation {
  PLUS("+") {
    double apply(double x, double y) { return x + y; }
  },
  MINUS("-") {
    double apply(double x, double y) { return x - y; }
  },
  TIMES("*") {
    double apply(double x, double y) { return x * y; }
  },
  DIVIDE("/") {
    double apply(double x, double y) { return x / y; }
  };
  private final String symbol;

  Operation(String symbol) {
    this.symbol = symbol; 
  }

  @Override public String toString() {
    return symbol; 
  }
  abstract double apply(double x, double y);
}

, Java 5 , . , , . , Operation.PLUS Operation.MINUS ..

, :

public static void main(String[] args) {
  double x = Double.parseDouble(args[0]);
  double y = Double.parseDouble(args[1]);

  for (Operation op : Operation.values())
    System.out.printf("%f %s %f = %f%n", x, op, y, op.apply(x, y));
}

Effective Java, Second Edition Joshua Bloch.

, Java " ". , . ; , " ", - . .

+3

You have not designers to transmit any arguments your subclasses, I do not understand how you think opt1, and opt2will be installed without any constructors with parameters. This is the wrong Java code right now.

0
source

Source: https://habr.com/ru/post/1793778/


All Articles