Hi All..

I'm a bit frustrated to understand what this interface inside that abstract class does.

This code is a part of the very first question in our future exam...

 

i'll be glad to hear what u think, and if u guys could give some examples to how can i use it, it would be wonderful.

 

public abstract class Expression {
  public abstract String toString();


  public abstract SimpleExpression evaluate();


  public void show() {
  System.out.println(this + " = " + evaluate());
  }


  protected static double toDouble(Object value) {
  return ((Number) value).doubleValue();
  }


  protected static interface Function {
  Object compute(Object... values);
  }
}









 

public class SimpleExpression extends Expression {
  private final Object value;


  public SimpleExpression(Object value) {
  this.value = value;
  }


  protected SimpleExpression(Function func, Expression... exprs) {
  Object[] values = new Object[exprs.length];
  for (int i = 0; i < exprs.length; i++)
  values[i] = exprs[i].evaluate().value;
  value = func.compute(values);
  }


  public String toString() {
       return value.toString();
  }


  public SimpleExpression evaluate() {
  return this;
  }
}








 

Thank you !!

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners