lua之自索引

Father={
    a=100,
    b=200
}

function Father:dis()
    print(self.a,self.b)
end

Father.__index=Father

Son=
{
    x1=300,
    x2=400
}

function Son:myprint()
    print(self.x1,self.x2)
end

s=setmetatable(Son,Father)

s:dis()

首先去Son表中寻找,发现没有dis,于是去他的元表中寻找,发现有dis,但是self是指Son,所以又会到Son表中寻找,发现Son表中依旧没有,于是去他的元表中寻找,最终找到。

原文地址:https://www.cnblogs.com/SunShine-gzw/p/13782514.html