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

OOP explained as simple as possible please?

Asked by 7 years ago
Edited 4 years ago

Introduction:

OOP, also known as Object Orientated Programming. I've been told that I should be using this in my scripts but I am not sure how it works. I've asked similar questions before and now I'd like to ask the community for help.

I've looked into one of lua's official chapters on OOP (Methods and creating functions and such) but I'm confused and is unable to implement OOP into my work.

What I'm Asking:

  1. What is OOP and what is the purpose of it?
  2. When should OOP be used over creating regular tables and functions and such?
  3. What are anonymous functions?
  4. How does self work?
  5. What's the difference between Methods and Functions?

While providing answers to these questions it would be amazing if I could be provided a couple examples such as cases where you may use OOP or just generally how I'd use it.

Again, I've tried to comprehend from the official sites and other tutorials but I really need this explained in a nutshell.

Thank you for any feedback to help me understand OOP.

1 answer

Log in to vote
1
Answered by 4 years ago

OOP


Ahh yes, the programming paradigm of object oriented programming.

the paradigm of OOP is defined as:

is a programming paradigm based on the concept of "objects", which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

by wikipedia

it can be thought of similar to real life objects, which have properties and functions(methods)

for example

class lamp
    boolean state

    method turnOn()
    method turnOff()

would be psuedocode for a potential lamp class that can model a real life lamp, which can be on or off, represented by the state property. The act of turning the lamp on and off can be represented by the turnOn and turnOff methods

Key terms associated with OOP includes

  • Inheritance
  • Encapsulation
  • Polymorphism

these i highly recommend you research in order to properly understand OOP

A positive of OOP it that it provides is a way to remove the assumptions of the condition of components; thus, the operations to be performed on that component and how to integrate it into the final product. (stackexchange)

Other positives of OOP can be found on the stackexchange post


OOP vs Procedural


(im assuming thats what you meant by "regular tables and such")

If you want to compare different programming paradigms, this page could be helpful in doing that,however, i want to list a few key differences

One of the more obvious ones is nomenclature , for example:

Procedural OO
Procedure Method
Module Class

One key difference is re-usability, as designs in OOP can be reused, however, the same cannot be said for procedural programs.


Anonymous functions


In computer programming, an anonymous function is a function definition that is not bound to an identifier. Or more simply, a function without a name

They are typically used as arguments which are passed to higher order functions.

examples of this in (roblox) lua include common format for event listeners

event:Connect(function(...)

end)

the function in there is an example of an anonymous function passed as an argument to the connect function, which connects the event to that function (hence the name connect).


self


putting it simply, self is just a variable name. According to the lua pil,

First, the function creates a table to keep the internal object state and stores it in the local variable self.

when refering to an "object constructor"

it is simply a variable name that is implicit to whatever table/userdata the value was called from with a colon (if that makes any sense)

like Object:Destroy(), or event:Connect()


Methods vs Functions


they both refer to a set of instructions that performs a task, the difference being that Methods are associated with an object, while functions are not.


This is getting a bit long-winded so ill try to keep this bit as short as possible. As you may or may not think is obvious, OOP isnt ubiquitous. One drawback is the difficulty of planning an OO program, as the flow isnt obvious when it comes to OO programs, thus increasing the time necessary and difficulty to write an OO program.

also if i stooged something up, be sure to tell me

0
several OOP jokes were killed during the making of this answer theking48989987 2147 — 4y
0
@theking48989987 could you show some examples of OOP and how it can be used? BlackOrange3343 2676 — 4y
Ad

Answer this question