Object-oriented Lua study notes

table of Contents

1. Blog introduction

2. Content

(1) Inheritance

(2) Multiple inheritance

3. Push

4. Conclusion


 

1. Blog introduction

This blog is one of the Lua study notes, introducing Lua object-oriented related content. This blog will implement inheritance and multiple inheritance through a few small examples. To view this blog, you need to have a relevant understanding of Lua's meta table. You can skip to this blog. Repost related articles on the blogger's meta table, with a portal at the bottom of the blog post.


2. Content

(1) Inheritance

After understanding the meta table, we know that when table B is the meta table of table A, when we call the method or attribute of table A, if we cannot find it, we will go to the meta method __index in table B to continue searching Related properties or methods, so we have found the idea of ​​inheritance, we first write a parent class table as follows:

local Parent = {}

function Parent:new()
	local proxy = {}
	self.__index = self
	setmetatable(proxy,self)
	return proxy
end

function Parent:print(str)
	print("父类输出:"..str)
end

The above code is very simple. We have a table Parent as the parent class. We wrote a method new() for the Parent. The focus of the method is self.__index = self. This operation points the parent's metamethod __index to the Parent itself, so The advantage of doing this is that if Parent is used as the meta table of other tables, the relevant attributes and methods will be found directly from the Parent, because the __index of the Parent points to itself, and then we use Parent as the meta table of the proxy table, and Passing proxy as the return value, let's take a look at the inheritance of subclasses:

local child1 = Parent:new()

child1:print("哈哈哈哈")

-----------------------------输出结果
父类输出:哈哈哈哈

function child1:print(str)
	print("子类1输出:"..str)
end

child1:print("哼哼哼哼")

-----------------------------输出结果
子类1输出:哼哼哼哼

In the above code, child1 is a new table obtained through the Parent method new. The metatable of this table is the Parent. The parent's print method will be called without rewriting. If it is rewritten, it is also very simple. Use the same method name. Just write it once, and call after rewriting is to execute the rewritten method.

 

(2) Multiple inheritance

The implementation of multiple inheritance is actually very simple. We implemented inheritance through the above example. We found that finding the parent class is actually looking for the meta method __index of the meta table. Our single inheritance above is to set __index to a table. Multi-inheritance is to set __index as a method, which will look up the method or attribute we need in multiple tables, and then return the found attribute or method.

--search方法用来从多个父类表中查找
local function search(key,list)
	for _,pValue in ipairs(list) do
		local v = pValue[key]
		if v then
			return v
		end
	end
end 

--createClass用来创建子类
function createClass(...)
	local proxy = {}
	local parents = {...}
	local meta = {
		__index = function(self,key)
			return search(key,parents)
		end
	}
	setmetatable(proxy,meta)
	return proxy
end


----------------------------------演示例子
local Parent1  = {}
local Parent2  = {}

function Parent1:print1()
	print("父类1输出")
end

function Parent2:print2()
	print("父类2输出")
end
local child1 = createClass(Parent1,Parent2)

child1:print1()
child1:print2()

----------------------------------输出
父类1输出
父类2输出

In the above code, we use the search method to filter the keys in multiple tables. In the createClass method, we set the meta method __index of the meta table of the table proxy to search. The parameters are the key to be searched and the multiple inherited. Table, so we can realize the idea of ​​multiple inheritance, you can refer to the demonstration example above.


3. Push

Github:https://github.com/KingSun5

Meta table: https://blog.csdn.net/Mr_Sun88/article/details/105205942


4. Conclusion

The blogger just briefly introduced the implementation ideas. More functions need to be continuously improved. If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the blogger’s ability is limited. If there are any errors in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/105720830