1. 程式人生 > >Lua程序設計之————Lua面向對象2

Lua程序設計之————Lua面向對象2

eight 9.png {} turn one 結果 none 程序設計 key

技術分享

技術分享

----------------------------------------------------------- Lua面向對象3
local smartMan = {
    name = "Tinywan",
    age = 26,
    money = 800000,
    sayHello = function()
        print("Tinywan say 大家好")
    end
}
local t1 = {}
local mt = {
    __index = smartMan,
    __newindex = function(table, key, value)
        print(key .. 
"字段不存在不要試圖給他賦值") end } setmetatable(t1, mt) t1.sayHello = function() print("HAHA") end t1.sayHello() --- 輸出結果 -- sayHello字段不存在不要試圖給他賦值 -- Tinywan say 大家好

技術分享

技術分享

----------------------------------------------------------- Lua面向對象3
local smartMan = {
    name = "none"
}
local other = {
    name 
= "大家好,我是無賴的table" } local t1 = {} local mt = { __index = smartMan, __newindex = other } setmetatable(t1, mt) print("other的名字,賦值前:" .. other.name) t1.name = "峨眉大俠" print("other的名字,賦值後:" .. other.name) print("t1 的名字:" .. t1.name) --- 輸出結果 -- other的名字,賦值前:大家好,我是無賴的table -- other的名字,賦值後:峨眉大俠 -- t1 的名字:none

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

有問題

技術分享

技術分享

技術分享

技術分享

local 變量不放在全局函數中去

技術分享

以上不需要 return 返回

技術分享

Lua程序設計之————Lua面向對象2