public static void main(String args[]) { /* * This sample shows the basic usage of the JEval Evaluator class. * Calling the default contructor will set he quoteCharater to single * quote. This constructor will also load all math variables, math * functions and string variables. */ Evaluator evaluator = new Evaluator(); try { /** * 添加变量到 Evaluator 类实例. */ evaluator.putVariable("a", "'Hello'"); evaluator.putVariable("b", "'World'"); /** * 简单输出变量. */ System.out.println(evaluator.evaluate("#{a}")); System.out.println(evaluator.evaluate("#{b}")); /** * 简单输出数学常量. */ System.out.println(evaluator.evaluate("#{PI}")); /** * 字符串拼装. */ System.out.println(evaluator.evaluate("#{a} + ' ' + #{b} + '!'")); /** * This sample clears the variables. This call will not clear * preloaded variables. */ evaluator.clearVariables(); /** * 自定义变量. */ evaluator.setVariableResolver(new MockVariableResolver()); System.out.println(evaluator .evaluate("#{MockVariable1} + #{MockVariable2}"));
/** * This sample shows an invalid expression. The variables were just * cleared, therefor the variable "a" no longer exists. */ System.out.println("An exception is expected in the " + "next evaluation."); System.out.println(evaluator.evaluate("#{a}")); } catch (EvaluationException ee) { System.out.println(ee); } }
* This sample shows the basic usage of the JEval Evaluator class.
* Calling the default contructor will set he quoteCharater to single
* quote. This constructor will also load all math variables, math
* functions and string variables.
*/
Evaluator evaluator = new Evaluator(); try { /**
* 添加变量到 Evaluator 类实例.
*/
evaluator.putVariable("a", "'Hello'");
evaluator.putVariable("b", "'World'"); /**
* 简单输出变量.
*/
System.out.println(evaluator.evaluate("#{a}"));
System.out.println(evaluator.evaluate("#{b}")); /**
* 简单输出数学常量.
*/
System.out.println(evaluator.evaluate("#{PI}")); /**
* 字符串拼装.
*/
System.out.println(evaluator.evaluate("#{a} + ' ' + #{b} + '!'")); /**
* This sample clears the variables. This call will not clear
* preloaded variables.
*/
evaluator.clearVariables();
/**
* 自定义变量.
*/
evaluator.setVariableResolver(new MockVariableResolver());
System.out.println(evaluator
.evaluate("#{MockVariable1} + #{MockVariable2}"));
/**
* This sample shows an invalid expression. The variables were just
* cleared, therefor the variable "a" no longer exists.
*/
System.out.println("An exception is expected in the "
+ "next evaluation.");
System.out.println(evaluator.evaluate("#{a}"));
} catch (EvaluationException ee) {
System.out.println(ee);
}
}
evaluator.putVariable("b", "'World'");variableName:是否可以设置成不是final?