Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Single inheritance

When using single inheritance, any class can inherit functionality from only one super class. To implement single inheritance, create a new class through an existing constructor. This can be a bit confusing, so let's see an example.

Start by creating an Animal class; this will be the base class:

Animal = {  sound = ""}Animal.new = function(self, object)    object = object or {}    setmetatable(object, self)    self.__index = self    return objectendAnimal.MakeSound = function(self)    print(self.sound)end

Not every animal is going to make the same sound. Create two new classes, dog and cat, that extend Animal:

-- Dog is a class, not an object (instance)Dog = Animal:new()Dog.sound = "woof"-- Cat is a class, not an Object (instance)Cat = ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content