我怎样才能减少这个脚本代码
这是示例代码。
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;
}
}
这是要执行的示例脚本。
p.setValue1(2);
p.setValue2(5);
如何减少脚本
setValue1(2);
setValue2(5);
编辑:我想在方法execute() 中运行这些javascript 脚本并使脚本更短。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“我是你”:P...我会添加一个新的构造函数...
类似:
If "I were you" :P... i will do add a new constructor...
something like: