How can I reduce this scripting code
Here is sample code.
package base;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Test {
int value1;
ScriptEngine engine;
public Test(){
this.engine = new ScriptEngineManager().getEngineByName("js");
this.engine.put("p",this);
}
public boolean execute(String script){
try {
if (script != ""){
this.engine.eval(script);
}
return true;
} catch (ScriptException e) {
e.printStackTrace();
return false;
}
}
public void setValue1(int v){
this.value1 = v;
}
public void setValue2(int v){
this.value2 = v;
}
}
And this is sample script to be execute.
p.setValue1(2);
p.setValue2(5);
How to reduce script to
开发者_开发技巧setValue1(2);
setValue2(5);
Edit: I want to run those javascript scripts in method execute() and make script shorter.
If "I were you" :P... i will do add a new constructor...
something like:
public Test(int value1, int value2){
super();
this.value1 = value1;
this.value2 = value2;
}
精彩评论