Git Product home page Git Product logo

eclass's Introduction

eclass

eclass is a simple object-oriented implementation of Lua

Example

base class

require("eclass")   -- declare class function

local Cbase = class("base") -- declare a class

function Cbase:_init_(name) --  constructor
    self.name = name  -- class member value.
end

function Cbase:hello()  -- class function
    return self.name
end

function Cbase:_del_()  -- destructor
    print("base del ... " .. self.name)
end

return Cbase

class one

require("eclass")
local Cbase = require("base")

Cone = class("one", Cbase)  -- inherit

function Cone:_init_(name)
    Cbase._init_(self, name)  -- supper call
end

function Cone:say()
    print("one start")
    print("one say " .. self.name)
    print("one end\n")
end

return Cone

class two

require("eclass")
local Cone = require("one")

CTwo = class("two", Cone)

function CTwo:_init_(name)
    Cone._init_(self, name)
end

function CTwo:say()
    print("two start")
    print("two say " .. self.name)
    print("call supper method.")
    Cone.say(self)
    print("two end\n")
end

return CTwo

class three

require("eclass")
local Cone = require("one")

CThree = class("three", Cone)

function CThree:_init_(name)
    Cone._init_(self, name)
    self._child = Cone.new("child") -- Recursive declaration.
end


function CThree:say()
    print("three start.")
    print("three say " .. self.name)
    print("child say.")
    self._child:say()
    print("three end.")
end

return CThree

test fucntion

package.path = package.path .. ";../src/?.lua;"

require("eclass")

local Cone = require("one")
local Ctwo = require("two")
local CThree = require("three")

local one = Cone.new("one")
local two = Ctwo.new("two")
local three = CThree.new("three")

print(one:hello())
assert(one:hello() == "one")
assert(two:hello() == "two")
assert(three:hello() == "three")

one:say()
two:say()
three:say()

one = nil
two:say()

eclass's People

Contributors

liaozhaoyan avatar

Stargazers

 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.