zoukankan      html  css  js  c++  java
  • lua中self.__index = self是什么意思?

    example1

    Account = {balance = 0}
    function Account:new(conf)
        conf = conf or {} 
        setmetatable(conf,self)
        self.__index = self
        return conf
    end
    
    • Account模拟一个对象,new方法类似于实例化一个对象,conf可存放一些初始值作用等同于confAccount的子类。

    • 解释:Account这个table有一个属性balance,通过new函数实例化一个类继承于Account。在lua中类,父类都是通过table数据结构加上元表元方法来实现。
      __index是lua一个元方法,被广泛的使用在模拟实现继承方法。访问一个table中不存在的key,lua将会返回一个nil。
      但一个表存在元表的时可能会发生改变。既访问不存在的key时,如果这个table存在元表,就会尝试在它的元表中寻找是否存在匹配key对应的value。

    • :是lua面向对象的语法糖。Account:new(conf)等同于Account.new(self, conf),相当于将调用者自身当做第一个参数,使用冒号调用就相当于隐式地传递self参数。

    • 元方法的定义可以写在new函数外面。如下:

    Account = {balance = 222}
    Account.__index = Account
    function Account:new(conf)
        conf = conf or {}
        setmetatable(conf, self)
        return conf
    end
    

    或者类似于lua_resty_mysql中写法:

    Account = {balance = 222}
    local mt = {__index = Account}
    function Account:new(conf)
        conf = conf or {}
        setmetatable(conf, mt)
        return conf
    end
    
    - 调用:
    
    local aa = Account:new()
    ngx.say(aa.balance)   --222
    

    example2

    local a = {}
    local b = {k = 11}
    setmetatable(a,b)  --设置元表,仅仅设置元表并不能使lua寻找父类的方法或属性
    b.__index = b          --设置元方法
    b.v = 22                  --给b表增加一个属性
    a.aa = 33                --给a表增加一个属性
    print(a.k)                --11, 返回父类b中k的值。
    print(a.v)                --22,父类增加的属性和方法都可以被子类继承
    print(b.aa)              --nil,相反,父类并不可以获取子类的值
    

    _call 索引, 它允许你把表当函数调用

    local t = setmetatable({}, {
        __call = function(t, a, b, c, d)
          return (a + b + c) * d
        end
      })
    
    local res = t(1,2,3,4)   --24
    
  • 相关阅读:
    win10 UWP button
    内网分享资源
    内网分享资源
    CF724F Uniformly Branched Trees
    win10 UWP FlipView
    win10 UWP FlipView
    win10 UWP FlipView
    搭建阿里云 centos mysql tomcat jdk
    搭建阿里云 centos mysql tomcat jdk
    win10 UWP 申请微软开发者
  • 原文地址:https://www.cnblogs.com/mentalidade/p/6561418.html
Copyright © 2011-2022 走看看