Lua初学习 9-13_02 元表与元方法

1:元表与元方法

       测试001:

student = {
name = "cocotang",
id = 10001,
calss = "math",
}
function student:SayHello(name)
print("student say hello to : "..name)
end

human = {
name = "human",
worldId = 10000001,
}
function human:SayHello(name)
print("human say hello to : "..name)
end

setmetatable(student,human)
human.__index=human
student:SayHello("pikaboo")

======================debug===================

student say hello to  : pikaboo

 

        测试002:

student = {
name = "cocotang",
id = 10001,
calss = "math",
}
function SayHello(name)
print("student say hello to : "..name)
end

human = {
name = "human",
worldId = 10000001,
}
function human:SayHello(name)
print("human say hello to : "..name)
end

setmetatable(student,human)
human.__index=human
student:SayHello("pikaboo")

======================debug=================

human say hello to : pikaboo

 

Q:student继承human( setmetatable(子table,父table) 父table.__index=父table)