热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

luaj加载自定义java,LuaJ如何在Lua脚本中创建Java对象的实例?

IhaveJavaclass:classSomeClass{privateinti;publicSomeClass(inti){this.ii;}}AndIneedtocreate

I have Java class:

class SomeClass{

private int i;

public SomeClass(int i){

this.i = i;

}

}

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?

解决方案

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)

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

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.



推荐阅读
author-avatar
mobiledu2502898543
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有