Forum

> > CS2D > Scripts > What's OOP?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch What's OOP?

2 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt What's OOP?

The Dark Shadow
User Off Offline

Zitieren
Yo, What's OOP? Can you show me an example to compare the difference between non-OOP and OOP?

alt Re: What's OOP?

ohaz
User Off Offline

Zitieren
Object Orientend Programming (OOP) is a programming paradigm where (almost) everything is handled as if it were an object that you can interact with. It's a huge thing, so it would be better to read books about it.

Examples in Lua:

OOP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Player = {posX = 0, posy = 0}

function Player:new (o, posX, posY)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    self.posX = posX or 0
    self.posY = posY or 0
    return o
end

function Player:move(x, y)
    self.posX += x
    self.posY += y
end

p = Player:new(nil, 0, 1)
p:move(2,5)

Non-OOP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function newPlayer(posX, posY)
    p = {
        posX = posX,
        posY = posY,
    }
    return p
end

function movePlayer(p, x, y)
    p.posX += x
    p.posY += y
end

p = newPlayer(0, 1)
movePlayer(p, 2, 5)
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht