Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

What exactly is object oriented programming? [closed]

Asked by
LuaQuest 450 Moderation Voter
8 years ago

I've recently ran into some terminology I'm not to familiar with along the lines of 'Object oriented programming' and 'Classes' and 'Objects'. I've heard It has something to do with tables, but it wasn't very clear to me. For example, replicating a table object with a variable like:

local original = {}

local repl = original

repl[1] = 'replicated to original table'

print(original[1]) -- 'replicated to original table'

But that's just what I've heard. Is there more to this? If anyone has the time or will to go a bit more in depth about object oriented programming (or even to give a small example), I'd really appreciate that. I guess you don't have to worry to much about over explaining, i kinda know how the rest of this works (i.e tables, variables, etc)

Locked by Zafirua, Avigant, and LifeInDevelopment

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
8
Answered by 8 years ago

OOP OOP OOP OOP OOP OOP

This question is hard to answer, why? Because it's not Lua specific only.

So how am I gonna answer?

With two languages of course!


Lua

Lua isn't actually an Object Oriented Programming language, which makes it a bit hard to use it. "But the official Lua website says something about OOP, so you are wrong!"

No, I am not, Lua may not be an OOP language, but it's a language that can REPLICATE OOP, using Metatables and Tables.

The most popular way to use OOP in Lua is by setting an __index of a table to another table of methods.

local tab = {} do
    local methods = {} --Being indexed by the new objects.

    tab.NewObject = function(obj)
        return setmetatable({}, {
            __index = methods
        })
    end
end

What this code does, is whenever the table that is returned by tab.NewObject is indexed, it returns methods.

This could be really useful if you use it correctly.

However, DigitalVeer calls that "Fake OOP", because it is not exactly OOP, it just tries to replicate it.


Python

This language is easy to use OOP in, because it is an Object Oriented Programming language!

They even have a key word in Python to start OOP, known as class.

Inheritance is another big thing in Python OOPing, in the above code(in the Lua) section, that is inheritance, instead of me trying to get something from this table, I got a thing from THAT table.

That is really confusing.

say I have two classes named Parent and Child, to inherit from Parent, the class Child, must have Parent in it's parenthesis

class Parent(object):

class Child(Parent): See how parent is in the parenthesis of Child? That is how Python knows you are trying to inherit from Parent.

So, what does Inheritance do?

Say I have a method in Parent, called eat, but I want Child to have that same method. Well, it does, because it inherited it from Parent.

Hope you liked this explanation, my laptop shutdown in the middle of writing it.

Ad
Log in to vote
11
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Objects

Object-oriented-programming (OOP) is a programming pattern that stores data and behaviors into "objects". An object is a state (properties or fields) combined with behaviors (usually methods).

OOP attempts to parallel the real-world in this way.

A dog object might have a .name. It might also have a :bark() method, or a :eat(food) method which affects its .hunger property.

Notice that this looks very similar to the way ROBLOX instances work. That's because the instances closely parallel the C++ code, which is very object-oriented.

Methods

Methods in Lua use :. It's just sugar -- dog:bark("arf") is the same thing as dog.bark(dog, "arf").

You can define methods normally:

function dog.bark(dog, message)
    print(dog.name .. " says " .. message)
end

or by using a : and the implicit self parameter:

function dog:bark(message)
    print(self.name .. " says " .. message)
end

Classes

A common (but not universal) aspect of object-oriented-programming is classes. A class of objects is a group of objects that are similar. For instance, there might be a class of animals, and a sub-class of dogs.

Usually, when you design OOP programs, you write the classes. The classes act like "blueprints" -- ways to make new objects.

Inheritance

A major component of classes is inheritance. A subclass (e.g. Dog) inherits from a superclass (e.g., Animal). That means that anything that can be done to the superclass can be done to the subclass.

For instance, in ROBLOX, BasePart provides things like .BrickColor and .Anchored, and :BreakJoints(), which subclasses like SpawnLocation can take advantage of.


Mutability = :(

A very common feature of OOP designs is mutation -- objects can change. Note that while this can be very convenient, it can also quickly make things messy. Immutable -- unchanging -- objects can be nice since they are more easily used in various ways.


OOP in Lua

You essentially have an object-oriented design if you follow these criteria (note: I am not advising you do this -- OOP isn't necessarily the best way to design, especially since Lua isn't really made for it)

  • No global variables (except classes), no global functions (except constructors)
  • Functions are always methods on objects
  • Constructor functions which create new objects. They should return a newly constructed object.

It might look like this:

Dog = {} -- Class's name is uppercase
function Dog:bark(message)
    print(self.Name .. " says " .. message)
end

-- Dog constructor
function Dog.new(name)
    local obj = {Name = name} -- set fields
    -- Give methods:
    obj.Bark = Dog.Bark
    -- Return!
    return obj
end

--------
local dog = Dog.new("Jeffrey")
dog:Bark("arf")

If you added a lot more methods, writing the constructor would start to become inconvenient.

Instead of attaching each method to the object, you can use the __index metamethod to make obj look like a Dog:

function Dog.new(name)
    local obj = {name = name}
    setmetatable(obj, {__index = Dog}) -- look at Dog for methods
    return obj
end

If you want to do inheritance, it's as simple as setting the metatable of the class:

setmetatable(Dog, {__index = Animal})

Then if Animal had a swim method, our dog object could also :swim.

0
Mutable objects make BlueTaslem sad. BlueTaslem 18071 — 8y
0
But your answer makes me very happy. LuaQuest 450 — 8y