GithubHelp home page GithubHelp logo

class.lua's Introduction

class.lua

This repository provides a code snippet that allows you to write classes in the Lua scripting language, compatible with Lua 5.3.

Features

  • Constructor and destructor (construct and destruct methods)
  • Inheritance (Class(BaseClass), SubClass.super)
  • Getters and setters (getXxx and setXxx methods and use xxx as the virtual properties)
  • instanceOf function

Usage

Just load the class.lua file in your Lua runtime environment and a function named Class is available. If you prefer another name, or want to require the file and return the function instead of creating a new global, feel free to modify the source code.

-- load `Class` into the global environment
require('class')

-- create a new class, named `BaseClass`
local BaseClass = Class()

-- constructor function must be named `construct`
function BaseClass:construct(text)
  print('Constructing BaseClass:', self)
  self.text = text
end

-- if this instance is being garbage collected, this is called
function BaseClass:destruct()
  print('Destructing BaseClass:', self)
end

-- regular method
function BaseClass:sayIt()
  print(self.text)
end

-- setter method must have the form `setXxx`
function BaseClass:setFoobar(value)
  print('Set foobar to:', value)
end

-- getter method must have the form `getXxx`
function BaseClass:getFoobar()
  print('Get foobar')
  return 27
end

local instance = BaseClass('Welcome class.lua!')
instance:sayIt() -- call regular method
instance.text = 'modified' -- modify regular property
instance.foobar = 'setting' -- uses setFoobar()
print(instance.foobar) -- uses getFoobar()
print(Class.instanceOf(instance, BaseClass)) -- true
print(instance:instanceOf(BaseClass)) -- true


local SubClass = Class(BaseClass)

function SubClass:construct()
  SubClass.super.construct(self, 'lorem ipsum dolor')
  -- or: BaseClass.construct(self, 'lorem ipsum dolor')
end

License

It's licensed under the MIT License.

class.lua's People

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.