我在luaj网站上做了一个简单的例子..LuaJ我正在尝试对当前正在使用的对象运行函数。但是luaJ正在制造一个新的物体。
我如何在当前对象上运行函数,而不是创建一个新对象。
考虑到下面的代码...
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.ZeroArgFunction;
import org.luaj.vm2.lib.jse.JsePlatform;
public class luaTest extends TwoArgFunction {
public luaTest() {}
changeInt mytest=new changeInt();
public LuaValue call(LuaValue modname, LuaValue env) {
LuaValue library = tableOf();
library.set( "countup", new countup(mytest) );
env.set( "luaTest", library );
return library;
}
void run() {
Globals globals = JsePlatform.standardGlobals();
mytest.setA(10); // Setting it 10 before calling the script
LuaValue chunk = globals.loadfile("script.lua");
chunk.call();
}
class countup extends ZeroArgFunction {
changeInt mytest;
public countup(changeInt a)
{
mytest=a;
}
public LuaValue call() {
return LuaValue.valueOf(mytest.countup());
}
}
}changeInt类只有一个变量...
public class changeInt {
int a = 1;
public int countup(){
return a++;
}
public void setA(int x)
{
a=x;
}
}luaScript很简单..
require 'luaTest'
print('count',luaTest.countup())
print('count',luaTest.countup())
print('count',luaTest.countup())有没有办法绕过它..
发布于 2015-06-23 14:24:48
是的,这对java程序员来说是非常琐碎的(我对java是个新手)..我使用了Singleton pattern,它解决了这个问题。
https://stackoverflow.com/questions/30972444
复制相似问题