Lua - Object Oriented
Introduction to OOP
Hello there, aspiring programmers! Today, we're going to embark on an exciting journey into the world of Object-Oriented Programming (OOP) using Lua. Don't worry if you're new to programming; I'll guide you through each step with the same care and enthusiasm I've used in my classroom for years.
Object-Oriented Programming is like building with LEGO bricks. Each brick (or object) has its own properties and can do certain things. When we put them together, we create amazing structures (or programs)!
Features of OOP
Before we dive into Lua, let's quickly look at the main features of OOP:
- Encapsulation: This is like keeping your toys in a box. The box (object) contains all the related stuff (data and functions).
- Inheritance: Imagine you have a robot toy. Now, you get a new robot toy that can do everything the old one can, plus some new tricks. That's inheritance!
- Polymorphism: This is like having a remote control that works for different devices. Same button, different actions depending on what you're controlling.
- Abstraction: Think of it as using a microwave. You don't need to know how it works inside; you just need to know which buttons to press.
OOP in Lua
Now, let's see how we can use these OOP concepts in Lua. Lua doesn't have built-in classes like some other languages, but we can create object-oriented structures using tables and functions. It's like we're building our own LEGO set from scratch!
Creating a Simple Class
Let's start by creating a simple class. In Lua, we use tables to represent classes.
local Dog = {}
Dog.__index = Dog
function Dog.new(name, age)
local self = setmetatable({}, Dog)
self.name = name
self.age = age
return self
end
function Dog:bark()
print(self.name .. " says: Woof!")
end
Let's break this down:
- We create a table called
Dog
. - We set
Dog.__index = Dog
. This is a bit of Lua magic that helps with inheritance. - We create a
new
function that acts as our constructor. It creates a new dog with a name and age. - We add a
bark
method to our Dog class.
Creating an Object
Now that we have our Dog class, let's create a Dog object:
local myDog = Dog.new("Buddy", 3)
Congratulations! You've just created your first object. myDog
is now a Dog object with the name "Buddy" and age 3.
Accessing Properties
We can access the properties of our dog like this:
print(myDog.name) -- Outputs: Buddy
print(myDog.age) -- Outputs: 3
It's as simple as using the dot notation!
Accessing Member Function
To make our dog bark, we can call the bark method:
myDog:bark() -- Outputs: Buddy says: Woof!
Notice the colon :
instead of a dot. This is Lua's way of passing self
to the method.
Complete Example
Let's put it all together:
local Dog = {}
Dog.__index = Dog
function Dog.new(name, age)
local self = setmetatable({}, Dog)
self.name = name
self.age = age
return self
end
function Dog:bark()
print(self.name .. " says: Woof!")
end
local myDog = Dog.new("Buddy", 3)
print(myDog.name) -- Outputs: Buddy
print(myDog.age) -- Outputs: 3
myDog:bark() -- Outputs: Buddy says: Woof!
Inheritance in Lua
Now, let's take our Dog class and create a more specific type of dog - a Labrador. This is where inheritance comes in handy.
local Labrador = {}
setmetatable(Labrador, {__index = Dog})
function Labrador.new(name, age, color)
local self = Dog.new(name, age)
setmetatable(self, {__index = Labrador})
self.color = color
return self
end
function Labrador:swim()
print(self.name .. " is swimming!")
end
Here, we're creating a Labrador class that inherits from Dog. It has all the properties and methods of Dog, plus a new property (color) and a new method (swim).
Over-riding Base Functions
We can also override functions from the base class. Let's give our Labrador a special bark:
function Labrador:bark()
print(self.name .. " says: Woof woof woof!")
end
Now, when a Labrador barks, it'll be different from a regular Dog.
Inheritance Complete Example
Let's put it all together:
local Dog = {}
Dog.__index = Dog
function Dog.new(name, age)
local self = setmetatable({}, Dog)
self.name = name
self.age = age
return self
end
function Dog:bark()
print(self.name .. " says: Woof!")
end
local Labrador = {}
setmetatable(Labrador, {__index = Dog})
function Labrador.new(name, age, color)
local self = Dog.new(name, age)
setmetatable(self, {__index = Labrador})
self.color = color
return self
end
function Labrador:swim()
print(self.name .. " is swimming!")
end
function Labrador:bark()
print(self.name .. " says: Woof woof woof!")
end
local myDog = Dog.new("Buddy", 3)
local myLabrador = Labrador.new("Max", 2, "Yellow")
myDog:bark() -- Outputs: Buddy says: Woof!
myLabrador:bark() -- Outputs: Max says: Woof woof woof!
myLabrador:swim() -- Outputs: Max is swimming!
And there you have it! We've created a basic class structure, implemented inheritance, and even overridden methods. You're well on your way to becoming a Lua OOP expert!
Remember, practice makes perfect. Try creating your own classes and experimenting with different properties and methods. Happy coding!
Credits: Image by storyset