无法调用类成员变量 #110

Closed
opened 2023-02-20 01:44:03 +00:00 by ReanRelay · 3 comments
ReanRelay commented 2023-02-20 01:44:03 +00:00 (Migrated from github.com)

kotlin 代码如下:

class Test {
    var name = 1

    fun testPrint(){
        println("调用 Kotlin 方法")
    }
}

 val globals = JsePlatform.standardGlobals()

 globals.set("Test", CoerceJavaToLua.coerce(Test()))
 //加载脚本
 val chunk = globals.load("print(Test.name)\nTest:testPrint()")
 chunk.call()

输出结果:
nil
调用 Kotlin 方法

为什么输出 Test 类中成员 name 为 nil ????????

kotlin 代码如下: ``` class Test { var name = 1 fun testPrint(){ println("调用 Kotlin 方法") } } val globals = JsePlatform.standardGlobals() globals.set("Test", CoerceJavaToLua.coerce(Test())) //加载脚本 val chunk = globals.load("print(Test.name)\nTest:testPrint()") chunk.call() ``` 输出结果: nil 调用 Kotlin 方法 为什么输出 Test 类中成员 name 为 nil ????????
e1roy commented 2023-02-21 13:20:40 +00:00 (Migrated from github.com)

You should first bind the Java class in the Lua file with full class name .

val chunk = globals.load('''
 local luajava = require("luajava")
 Test = luajava.bindclas("Test")
 Test.new():testPrint();
''')

If you use Kotlin, maybe you should bind classes with TestKt instead of Test.

More examples : Click me

You should first bind the Java class in the Lua file with **full class name** . ```kotlin val chunk = globals.load(''' local luajava = require("luajava") Test = luajava.bindclas("Test") Test.new():testPrint(); ''') ``` If you use Kotlin, maybe you should bind classes with `TestKt` instead of `Test`. More examples : [Click me](https://github.com/luaj/luaj/blob/daf3da94e3cdba0ac6a289148d7e38bd53d3fe64/examples/android/assets/activity.lua)
ReanRelay commented 2023-02-21 13:42:37 +00:00 (Migrated from github.com)

You should first bind the Java class in the Lua file with full class name .

val chunk = globals.load('''
 local luajava = require("luajava")
 Test = luajava.bindclas("Test")
 Test.new():testPrint();
''')

If you use Kotlin, maybe you should bind classes with TestKt instead of Test.

More examples : Click me

You don't seem to see my question clearly. I asked why the calling class member variable is nil

> You should first bind the Java class in the Lua file with **full class name** . > > ```kotlin > val chunk = globals.load(''' > local luajava = require("luajava") > Test = luajava.bindclas("Test") > Test.new():testPrint(); > ''') > ``` > > If you use Kotlin, maybe you should bind classes with `TestKt` instead of `Test`. > > More examples : [Click me](https://github.com/luaj/luaj/blob/daf3da94e3cdba0ac6a289148d7e38bd53d3fe64/examples/android/assets/activity.lua) You don't seem to see my question clearly. I asked why the calling class member variable is nil
e1roy commented 2023-02-23 09:51:34 +00:00 (Migrated from github.com)

If you want to use Test.name in Lua, you may need to add @JvmField to the field ( btw : getName() is better ).

    class Test {

        @JvmField
        var name = 1

        fun testPrint() {
            print("调用 Kotlin 方法")
        }
    }

If you decompile the bytecode, you will see that the field is compiled as private.

Test {
    private int name = 1;
    
    public final int getName() {
      return this.name;
    }
    
    public final void setName(int <set-?>) {
      this.name = <set-?>;
    }
    
    public final void testPrint() {
      System.out.print("Kotlin );
    }
  }
If you want to use `Test.name` in Lua, you may need to add `@JvmField` to the field ( btw : `getName()` is better ). ```kotlin class Test { @JvmField var name = 1 fun testPrint() { print("调用 Kotlin 方法") } } ``` If you decompile the bytecode, you will see that the field is compiled as private. ```kotlin Test { private int name = 1; public final int getName() { return this.name; } public final void setName(int <set-?>) { this.name = <set-?>; } public final void testPrint() { System.out.print("Kotlin ); } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: open-autonomous-connection/luaj#110