lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am referring to PIL II, 16.4, wondering how to implement inheritance with this class-scheme. Taking the example on page 157:

function newAccount (initialBalance)
  local mystuff = {balance = initialBalance}

  local withdraw = function (v)
    mystuff.balance = mystuff.balance - v
  end

  -- other functions omitted

  return {
    withdraw = withdraw
  }
end

I would implement a descendant of this class like so:

function newSpecialAccount (initialBalance)
  local self = newAccount (initialBalance)

  -- local additional data ("mystuff")

  local someFunction = function ()
    -- do something
  end

  self.someFunction = someFunction
  return self
end

Questions: Is this the way to do this? If yes, it means that the descendant class is restricted to the same public interface to its parent as any other client, right?

Thanks and regards
- Stefan