Inheritance 基于分类的继承在Lua中是如何工作的?

Inheritance 基于分类的继承在Lua中是如何工作的?,inheritance,lua,Inheritance,Lua,我一直在读Lua的教科书,但我不太了解如何实现它。假设我有两门课: Hero = {} function Hero.new(n) local self = {name = n, health = 100} local dealDamage = function(self) return 10 end local takeDamage = function(self, h) self.health = self.health - h end loca

我一直在读Lua的教科书,但我不太了解如何实现它。假设我有两门课:

Hero = {}

function Hero.new(n)
  local self = {name = n, health = 100}

  local dealDamage = function(self)
    return 10
  end

  local takeDamage = function(self, h)
    self.health = self.health - h
  end

  local getHealth = function(self, h)
    self.health = self.health + h
  end

  local getName = function(self)
    return self.name
  end

  local isDead = function(self)
    if self.health <= 0 then
      return true
    end
  return false
  end

   local __tostring = function(self) 
    return "Hero[Name: "..self.name..", health: "..self.health.."]" 
  end

  local mt = {
    __tostring = __tostring
  }
  return setmetatable({
    name = self.name,
    health = self.health,
    dealDamage = dealDamage,
    takeDamage = takeDamage,
    getHealth = getHealth,
    getName = getName,
    isDead = isDead
  }, mt)
end
return Hero
我认为我做错了的是类是如何用元表构造的。通过一些例子,比如,或者其他几个问题,我可以看出我们的课程是非常不同的


如何正确设置继承,让Fighter类继承Hero类的函数和数据字段

在Lua中定义类有许多不同的方法。与其选择OOP系统作为“合适”的方式,不如让我们看看您拥有什么,并尝试定义您的系统应该如何工作

您的
Hero
类定义了一个构造函数,该构造函数返回一个对象,其中所有方法和实例变量都直接包含在该对象上。这是一个非常简单的概念。我要做的唯一主要更改是从顶部删除
self
变量,因为您并没有真正使用它:

function Hero.new(name)
  local dealDamage = function(self)
    return 10
  end

  local takeDamage = function(self, h)
    self.health = self.health - h
  end

  local getHealth = function(self, h)
    self.health = self.health + h
  end

  local getName = function(self)
    return self.name
  end

  local isDead = function(self)
    return self.health <= 0
  end

  local __tostring = function(self) 
    return "Hero[Name: "..self.name..", health: "..self.health.."]" 
  end

  local mt = {
    __tostring = __tostring
  }
  return setmetatable({
    name = name,
    health = 100,
    dealDamage = dealDamage,
    takeDamage = takeDamage,
    getHealth = getHealth,
    getName = getName,
    isDead = isDead
  }, mt)
end
该系统的优点是:

  • 与大多数系统相比,间接性更少。您可以只查看对象中的方法,而不是查找类表

  • 继承是灵活的。您可以通过修改构造函数来更改它的工作方式。您可以复制方法而不是使用
    \uuu index
    ,或者您可以在构造函数中实现多重继承

  • 我把这个系统的缺点留给读者作为练习

    function Hero.new(name)
      local dealDamage = function(self)
        return 10
      end
    
      local takeDamage = function(self, h)
        self.health = self.health - h
      end
    
      local getHealth = function(self, h)
        self.health = self.health + h
      end
    
      local getName = function(self)
        return self.name
      end
    
      local isDead = function(self)
        return self.health <= 0
      end
    
      local __tostring = function(self) 
        return "Hero[Name: "..self.name..", health: "..self.health.."]" 
      end
    
      local mt = {
        __tostring = __tostring
      }
      return setmetatable({
        name = name,
        health = 100,
        dealDamage = dealDamage,
        takeDamage = takeDamage,
        getHealth = getHealth,
        getName = getName,
        isDead = isDead
      }, mt)
    end
    
    -- We need the same parameter as Hero.
    function Fighter.new(name)
      -- Create a base object by instantiating Hero.
      local hero = heroClass.new(name)
    
      local __tostring = function(self) 
        return "Fighter[Name: "..self.name..", health: "..self.health.."]" 
      end
    
      local mt = {
        __tostring = __tostring,
        -- Inherit from the base object.
        __index = hero,
      }
      return setmetatable({
          strength = 3,
      }, mt)
    end