From 42ec7286b2d36a9ba22925f816a17cb1cc2aa5ce Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 30 Oct 2021 11:32:16 +0800 Subject: + Penlight --- Data/Libraries/Penlight/tests/lua/animal.lua | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Data/Libraries/Penlight/tests/lua/animal.lua (limited to 'Data/Libraries/Penlight/tests/lua/animal.lua') diff --git a/Data/Libraries/Penlight/tests/lua/animal.lua b/Data/Libraries/Penlight/tests/lua/animal.lua new file mode 100644 index 0000000..9366db9 --- /dev/null +++ b/Data/Libraries/Penlight/tests/lua/animal.lua @@ -0,0 +1,54 @@ +-- Module containing classes +local class = require 'pl.class' +local utils = require 'pl.utils' +local error = error +if utils.lua51 then + module 'animal' +else + _ENV = {} +end + +class.Animal() + +function Animal:_init(name) + self.name = name +end + +function Animal:__tostring() + return self.name..': '..self:speak() +end + +class.Dog(Animal) + +function Dog:speak() + return 'bark' +end + +class.Cat(Animal) + +function Cat:_init(name,breed) + self:super(name) -- must init base! + self.breed = breed +end + +function Cat:speak() + return 'meow' +end + +-- you may declare the methods in-line like so; +-- note the meaning of `_base`! +class.Lion { + _base = Cat; + speak = function(self) + return 'roar' + end +} + +-- a class may handle unknown methods with `catch`: +Lion:catch(function(self,name) + return function() error("no such method "..name,2) end +end) + +if not utils.lua51 then + return _ENV +end -- cgit v1.1-26-g67d0