Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/coin8086/lua-class-lib
A "class" keyword to Lua 5.1!
https://github.com/coin8086/lua-class-lib
Last synced: 9 days ago
JSON representation
A "class" keyword to Lua 5.1!
- Host: GitHub
- URL: https://github.com/coin8086/lua-class-lib
- Owner: coin8086
- Created: 2015-04-01T04:08:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-07T01:44:26.000Z (over 7 years ago)
- Last Synced: 2023-02-28T23:11:58.996Z (over 1 year ago)
- Language: Lua
- Homepage:
- Size: 30.3 KB
- Stars: 7
- Watchers: 0
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
_See my blog post http://huiming.io/2011/06/01/lua-oop.html for more on OOP in Lua and the lib._
# lua-class-lib
A "class" keyword to Lua 5.1!Lua Class Lib is an OOP tool for Lua. It provides a class 'keyword' to help you do OOP in Lua in an intuitive way. It's similar to OOP in Python but in Lua idiom. So you may feel familiar if you know Python. However you're not required to know Python to use it.
# Class Definition and Instantiation
```
require 'cls'class 'Worker' --define class Worker
{
__init__ = function(self, id) --initializer
self.id = id --initialize instance member
end;showId = function(self) --method
print('My worker id is ' .. self.id .. '.')
end
}w = Worker(100) --create an object of Worker
w:showId() --call Worker's method
```Output:
```
My worker id is 100.
```# Inheritance and Polymorphism
```
require 'cls'--class 'Worker' ... defined as above
class 'Person'
{
__init__ = function(self, name)
self.name = name
end;say = function(self)
print('Hello, my name is ' .. self.name .. '.')
self:saySthElse()
end;saySthElse = function(self) --will be override later
end
}class 'Employee: Person, Worker' --Employee inherits Person and Worker
{
__init__ = function(self, name, salary, id)
Person.__init__(self, name) --call base's initializer directly
Worker.__init__(self, id)
self.salary = salary
end;saySthElse = function(self)
print('My salary is ' .. self.salary .. '.')
end
}e = Employee('Bob', 1000, 1)
e:say()
e:showId()```
Output:
```
Hello, my name is Bob.
My salary is 1000.
My worker id is 1.
```# Next
See https://github.com/coin8086/lua-class-lib/wiki/API-Reference for API details.