zoukankan      html  css  js  c++  java
  • Lua程序设计(二)面向对象概念介绍

    ----------------------------------------------------------- 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 返回

  • 相关阅读:
    团队冲刺第四天
    团队冲刺第三天
    团队冲刺第二天
    团队冲刺第一天
    全球疫情地图显示
    团队博客——keep running视频+PPT介绍
    周总结7
    人月神话阅读笔记03
    Java枚举类型的使用,数值的二进制表示
    四则运算器
  • 原文地址:https://www.cnblogs.com/tinywan/p/6947551.html
Copyright © 2011-2022 走看看