开发者

How can I reduce this scripting code

开发者 https://www.devze.com 2023-01-09 04:12 出处:网络
Here is sample code. package base; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager;

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号