修改参数

类 / 方法

local Log = {}
Log.__index = Log

function Log.new()
  return setmetatable({last_data = ""}, Log)
end

function Log:print_max5(data)
  self.last_data = data
  -- Only the parameter gets the new value:
  -- the field keeps the whole string
  data = data:sub(1, 5)
  print(data)
end

local log = Log.new()
-- prints 12345
log:print_max5("1234567")
-- log.last_data = 1234567
print("log.last_data = " .. log.last_data)

-- sub never fails past the end
-- prints 123
log:print_max5("123")