[LUA] metatable, metamethod, object-oriented

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right

Article directory


foreword

During the interview with Tencent, I asked about the content of Lua, which mainly focused on hot updates, metatables, and metamethods. Because I stepped on the pit, I planned to write an article to record it.


1. Meta table

This article introduces the metatable, and the metatable table is the only data structure in Lua. Table can be used as many things in my use. It can implement classes, inheritance, and structure. Any element can be added to the table.

t= {

    a = 10,

    b = 100,

    c = "abc"

}

This table is a bit like a structure

2. Meta method

The existence of metamethods is to allow our metatables to interact with each other, and metamethods allow specific operations to be performed between assigned metatables

t = {
    a = 10,
    b = 100,
    c = "abc"
}
mt = {
    __add = function(a,b)
        return a.a + b
    end,
}
setmetatable(t, mt)

print(t + 2)

Among them, mt is a metamethod, and the setmetatable method will assign the methods in mt to the t metatable. In this way, the t method can be operated. Here is the lua document. The detailed metamethod content can be found in the lua document. In my opinion, it is a bit like callback . own function

t = {
    a = 10,
    b = 4,
    c = "abc",
    __my = function(a,b)
        return a + b
    end
}
print(t.__my(t.a,t.b))

personal opinion

In my opinion, lua is a caller role, because the biggest feature of lua as a scripting language is that it does not need to be compiled, so each modification does not need to be re-executed. This feature is very important for games that have been released. Every modification of an online project needs to be reviewed again. After calling lua, the feature that does not need to be compiled saves a lot of trouble. It is necessary to modify bugs and upload resources. , combined with no need to compile can save a lot of debugging time.

Three, object-oriented

We talked about metatables and metamethods above. We regard metatables as objects, and we write functions in metamethods, and assign metamethods to objects to complete object-oriented. Let’s take an example first.

bag = {}
bagmt = {--元方法
    put = function(t,item)
        table.insert(t.items,item)
    end,
    take = function(t)
        return table.remove(t.items,item)
    end,
    list = function(t)
        return table.concat(t.items,",")
    end,
    clear = function(t)
        t.items = {}
    end,
}
bagmt["__index"] = bagmt--这里是将index元方法赋予自己,作用是调用不存在的函数时会在bagmt里找
function  bag.new()--构造函数
    local t = {
        items = {}
    }
    setmetatable(t,bagmt)
    return t
end
local b = bag.new()
b:put("apple")
b:put(666)
b:put(1)
print(b:list())

It can be seen here that after the constructor is written, the metamethod is given to the constructed metatable in the constructor, and the table becomes an object, and the object-oriented operation can be completed. The author learned C++ before, and here you will find that the constructor is actually the same. If the constructor is the original parent class, then you still need to write functions inside, but lua is written outside and then assigned, and the result is the same.

If objects need to interact with each other, they may need to originate from the same constructor, and then write the corresponding metamethod.

Summarize

In my opinion, the metatable is the only data structure in Lua, but this data structure can be used as a class or structure, and after the metamethod is called, it can also be given the corresponding operation function, so as to achieve the operation of the metatable as an object and achieve object-oriented operation.

Guess you like

Origin blog.csdn.net/hoxidohanabi/article/details/128133293