Skip to content

Commit

Permalink
oop example
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasvlevi committed May 9, 2024
1 parent 53c40b3 commit 680a9df
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/oop.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local Vector = class('Vector');

function Vector:init(x, y)
self.x = x;
self.y = y;
return self;
end

function Vector:copy()
return Vector:new(self.x, self.y);
end

function Vector:add(other)
self.x = self.x + other.x;
self.y = self.y + other.y;
end

function Vector:print()
print(self.x, self.y);
end

local a = Vector:new(3, 4);
local b = Vector:new(5, 6);

a:print();
b:print();

local c = a:copy();
c:add(b);

c:print();
a:print();

0 comments on commit 680a9df

Please sign in to comment.