LuaJ如何在Lua脚本中创建Java对象的实例? [英] LuaJ How to create instance of Java object in Lua script?

查看:351
本文介绍了LuaJ如何在Lua脚本中创建Java对象的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java类:

class SomeClass{
 private int i;
 public SomeClass(int i){
  this.i = i;
 }
}

我需要使用LuaJ库在Lua脚本中创建此类的实例,并将其传递给Java函数.我该怎么办?

And I need to create instance of this class in Lua script and pass it to Java function, using LuaJ library. How I can do it?

推荐答案

这是在lua.org上找到的一些示例代码:

This is some example code found on lua.org:

jframe = luajava.bindClass( "javax.swing.JFrame" )
frame = luajava.newInstance( "javax.swing.JFrame", "Texts" );
frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE)
frame:setSize(300,400)
frame:setVisible(true)

来源: http://www.luaj.org/luaj/3.0/README.html#luajava

以您的示例为例,这应该翻译为:

With your example, this should translate to:

local obj = luajava.newInstance("your.package.SomeClass",123)
print(obj.i) --> nil -- Since it's a private field

如果有方法public int getValue(),则可以使用obj:getValue().

If you have a method public int getValue(), you can use obj:getValue().

我已经很长时间没有进行自我测试了.

I've tested this myself just now, as I hadn't for a long time.

这篇关于LuaJ如何在Lua脚本中创建Java对象的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆