Skip to content

Custom Features

qtiuto edited this page Jan 1, 2019 · 3 revisions
  • Object call

An Object can be call with a table to set its fields. Given a class like

class A{
public int idx;
public String str;
}

then

local a=A(){idx=3,str='ggg'}

is equivalent to

local a=A();
a.idx=3 
a.str='ggg'

Never use this syntax with List and its implementation types,ArrayList(){1,2,4} won't work. Use ArrayList{1,2,4} instead. However, you can use things like HashMap(){r=4,g=5,b=6}. However, I still advise to use constructor syntax rather than Object call. This feature is for Bean class.

To allocate a Bean class, you can use sun.misc.unsafe to achieve your goal.

  • Extending methods

    Add a custom lua function as a object method is allowed. The syntax is followed

using 'java.lang'
Object.kk= function(obj,...)--... stands for args,ojb stands for the instance
   print(...)
end
Object().kk(2,3,5) -- prints 2 3 5

Note

  1. Only object method will be add.
  2. The method is add to the type.
  3. The method is only available for current thread.
  4. The method won't be remove until its thread dies.
  5. The method is only available for lua code. Java side will never notice them.
  6. Never type code like Object.kk=function() end; Object.kk()

These features are inspired by c#, so I doesn't advice you to use them for java code.

Clone this wiki locally