SoFunction
Updated on 2025-04-07

Lua Basic Tutorial Table (Table) Study Notes

surface

Copy the codeThe code is as follows:

    a = { }
    b = { x = 1, ["hello, "] = "world!" }
    = "ni, hao!"
    a[1] = 100
    a["a table"] = b
    function foo()
    end
    function bar()
    end
    a[foo] = bar
--Exhaust tables a and b respectively
    for k, v in pairs(a) do
        print(k, "=>", v)
    end
    print("----------------------------")
    for k, v in pairs(b) do
        print(k, "=>", v)
    end

Copy the codeThe code is as follows:

--Output result
1 => 100
a table => table: 003FB3A0
function: 003FCBB0 => function: 003FCBD0
astring => ni, hao!
----------------------------
hello,  => world!
x => 1

How to define a table: a = {}, b = {…}

Members of the access table:Access members of a table through the "." or "[]" operator. Note: The expression is equivalent to a["b"], but not to a[b]

Copy the codeThe code is as follows:

    local b = {"h", 20} --《=》 {[1]="h", [2]=20}  《=》local b={};b[1]="h";b[2]=20
    local d = {x=0, y=0}  --《=》local d = {}; =0; =0 《=》local d = {}; d["x"]=0; d["y"]=0

Keys and values ​​of table items:Any type of variable, except nil, can be used as keys to table items. From simple values, strings to complex functions, tables, etc., it is possible; similarly, any type of variable, except nil, can be used as the value of the table entry. Assigning nil to the value of a table entry means deleting this item from the table. For example, let = nil, delete the item with the key "b" in table a. If you access a non-existent table entry, its value is also nil. For example, there is c = but there is no entry with the key "b" in table a, then c is equal to nil.

A simple object implementation method

Copy the codeThe code is as follows:

function CreateStudent(ID,Name)
   local Obj={id=ID,name=Name};
    function Obj:GetID()
    return ;
 end
 function (self)
    return ;
 end
 function Obj:SetID(ID)
    =ID;
 end
 =function(self,Name)
 =Name
 end
 return Obj;
end

s1=CreateStudent(1,"andy");
print("s1's1'name=",(s1))

s1:SetID(2);
(s1,"lili");
print("s1's1'name=",s1:GetName())

--Output result
--s1'id= 1 s1'name= andy
--s1'id= 2 s1'name= lili

Object factory pattern:The create function like the above code

Use tables to represent objects:Put the data and methods of the object in a table. Although there are no private members hidden, it is completely acceptable for simple scripts.

Definition of member methods:function obj:method(a1, a2, ...) … end <==>function (self, a1, a2, ...) … end <==> = function (self, a1, a2, ...) … end
Calling member methods:obj:method(a1, a2, …) <==>(obj, a1, a2, ...)

Simple inheritance

Copy the codeThe code is as follows:

function CreateStudent(ID,Name)
   local Obj={id=ID,name=Name};
    function Obj:GetID()
    return ;
 end
 function (self)
    return ;
 end
 function Obj:SetID(ID)
    =ID;
 end
 =function(self,Name)
 =Name
 end
 return Obj;
end

function CreateClassStudent(ID,Name,Class)
   local obj=CreateStudent(ID,Name);
    =4;
   function obj:GetClass()
      return ;
   end
   function (self,class)
      =class;
   end
   return obj;
end

s2=CreateClassStudent(1,"andy",5);
print("s2'class=",(s2))

Advantages: Simple, intuitive Disadvantages: Traditional, not dynamic enough