Reading this article requires understanding the previous one Metamethods in Lua
1, Understand the meaning of
When called through: the system will automatically pass the current table to self, for example, a.eat(a) is equivalent to a:eat()
Passing the current object to the eat method improves the extensibility of the table method.
-- The essence is use table Simulate
--Defining an empty table is equivalent to a class
Person = {}
---Define local table reference variables to reduce the coupling of method reference table fields
this =Person
--Definition field
Person.Name="Bob"
Person.Gender = "male"
Person.Age = 18
--Definition method
--First method (anonymous function)
Person.Speak = function()
print("People are talking.")
end
--Second kinds
function Person.Walking()
print("People are walking.")
end
--Method invocation method
function Person:showInfo()
print("Call personal information")
print("Name:"..this.Name)
print("Age:"..this.Age)
end
--Use self Keyword function usage":"Defined function
function Person:show()
print("Call personal information")
print("Name:"..self.Name)
print("Age:"..self.Age)
end
--call
print(Person.Name)
print(Person.Gender)
print(Person.Age)
Person.Speak()
Person.Walking()
a = Person
Person = nil
a.showInfo()
--Amount to a.showInfo(a)
a:showInfo()
2, New new object
In the first part, we solved the problem of universality of the table method, but if we want to create a new Person table object, we have to rewrite a table and reassign it, which is very troublesome. So in Lua, we define a prototype first. By defining a new method, we can easily create a prototype object (or a table in Lua).
Player = {
x = 0, y = 0,
name = "",
new = function ()
p = {}
for k,v in pairs(Player) do
p[k] = v
end
return p
end,
--Error demonstration 1
--move = function(x,y)
-- Player.x = Player.x + x
-- Player.y = Player.y +y
--end
--Method 1
--[[ move = function(p,x,y)
p.x = p.x+x
p.y = p.y +y
end]]
}
--Method two self To cooperate with ":"Use
function Player:move(x,y)
self.x = self.x +x
self.y = self.y +y
end
p1 = Player.new()
p1.x = 10;
p1.y = 20;
p1.name = "Bob"
p2 = Player.new()
p2.x = 30;
p2.y = 40;
p2.name = "Steve"
print(p1.x,p1.y,p1.name)
print(p2.x,p2.y,p2.name)
--Here it shows“:"Pass itself in as a parameter
p1:move(10,10)
p2:move(10,10)
print(p1.x,p1.y,p1.name)
print(p2.x,p2.y,p2.name)
3, Use to inherit
Persion = {name=0,age=0}
--The index of redefinition meta table must have
Persion.__index = Persion
--Simulation structure, general name is new()
function Persion:new(name,age)
local self = {}
--self It's a normal watch, Persion It's a meta table. It's a meta table self Table lookup index does not exist, but meta table Persion Defined in__index Method, it will go__index Find in the specified table
setmetatable(self, Persion) --Must have
self.name = name
self.age = age
return self
end
function Persion:tostring()
print(self.name,self.age)
end
function Persion:work()
print(self.name.."My job is to provide for the aged")
end
--super Of self Parameter is A His watch is Persion
super = Persion:new("super",99)
super:tostring()
super:work()
--son Inherit from super,But this time new Method, self Parameter is son,The meta table is super,__index Also super
son=super:new("son",18)
son:tostring()
son:work()
--son Rewrite super Method
function son:work()
print(self.name.."My job is to drive")
end
son:work()
Output results: