diff options
author | Aki <nthirtyone@gmail.com> | 2017-08-13 02:33:01 +0200 |
---|---|---|
committer | Aki <nthirtyone@gmail.com> | 2017-08-13 02:33:01 +0200 |
commit | d1e722130257781f7db796f32a7d25dcb5c4afba (patch) | |
tree | 3aa368804735df2c0d7d4183c12f32a8934e0885 /example.lua | |
parent | 0866dec8387a5ce6d5a547338147beda932114dc (diff) | |
download | object-master.zip object-master.tar.gz object-master.tar.bz2 |
Example in readme doesnt need duplicate
Diffstat (limited to 'example.lua')
-rw-r--r-- | example.lua | 32 |
1 files changed, 0 insertions, 32 deletions
diff --git a/example.lua b/example.lua deleted file mode 100644 index d0f6a72..0000000 --- a/example.lua +++ /dev/null @@ -1,32 +0,0 @@ -local Object = require "Object" - -local Point = Object:extends() -- creating new class by extending basic class -function Point:new (x, y) -- defining function called by constructor for newly created class - self.x = x -- instance created by constructor is `self` - self.y = y -end -- no returns needed - -Point.NUMBER = 10 -- setting some global values for class - -function Point:__tostring () -- metamethod used with `tostring()` - return "Point<" .. self.x .. ", " .. self.y .. ">" -end - -local a = Point(3, 2) -- create new instance by calling class -print(tostring(a)) - -assert(a:is(Point)) -- you can also check if object is instance of class -assert(a:is(Object)) -- use `is()` method for it - -local Foo = Point:extends() -- you can extend whatever class you want -function Foo:new (str, x, y) - Foo.__super.new(self, x, y) -- call super constructor first - self.str = str -- do whatever else is left to be done -end - -function Foo:__tostring () -- metamethods are not inherited - return "Foo(" .. self.str .. ")<" .. self.x .. ", " .. self.y .. ">" -end - -local b = Foo("lorem", 7, -1) -print(tostring(b)) |