JAVA动态属性条件求值

标签 java oop conditional-statements evaluation

我有一个用例,其中一个对象带有一个字符串,该字符串具有针对另一个对象的属性进行评估的条件。 例如。 StudentEvaluator 有条件

student.pastScore>70 && student.currentScore>90 && student.sportsParticipation=true

Student对象有各自的属性。

E.g. pastScore, currentScore and sportsParticipation

现在 StudentEvaluator 条件字符串是在运行时创建的,必须评估是真还是假。 有许多 StudentEvaluator 在不同条件下并行运行。 现在 StudentEvaluator 在其评估函数中接受学生参数并评估条件。 即

public Class StudentEvaluator{

String condition;

  public StudentEvaluator(String condition){
    this.condition = condition;
  }

  evaluate(Student student){
    <<Code>>
  }

}

什么是最有效的评估方法? 欢迎任何开箱即用的想法! :)

最佳答案

过去做过。您有多种选择

  1. OGNL
  2. MVEL - 比 OGNL 更高效。支持 logical and conditional operators.
  3. SpEL - Spring Expression Language - 我的偏好。

关于JAVA动态属性条件求值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13311277/

相关文章:

java - 如何为程序中间加载创建第二个启动屏幕?

相互依赖的类的 C++ 继承

python - 从列表中删除一个元素(从列表列表中删除行)

java - Primefaces 搜索 mysql

java - 购买列表中的项目 - 线程中出现异常 "main"java.lang.IndexOutOfBoundsException : Index: 0, 大小:0

Java 语法 >>, &, ?和 :

python - Pandas 数据框 : duplicates based on column and time range

php - 为什么我们在 PHP 中将一些变量声明为 "$_variablename"...?

c - 面向对象的C无法编译

javascript - 您可以根据视口(viewport)大小制作条件语句来使用或不使用某些 JavaScript 吗?