Skip to content

Felyp-Henrique/class

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lua Class

Distribution

This library is a simple implementation for OOP in Lua. It's inspired by the Java and Python. It's works with classes, inheritance and polymorphism.

  • It's works with constructors.
  • It's works with instance methods.
  • It's works with static methods and fields.
  • It's has multiple inheritance support.
  • It's has polymorphism support.
  • It's has super keywords.
require 'class'

Point = class {
  BEGIN_X = 0,
  BEGIN_Y = 0,
  
  new = function (self, x, y)
    self.x = x or Point.BEGIN_X
    self.y = y or Point.BEGIN_Y
  end,

  distance = function (self, other)
    return math.sqrt((self.x - other.x)^2 + (self.y - other.y)^2)
  end,

  create_with_x = function (x)
    return Point:new(x, Point.BEGIN_Y)
  end,
}

local p1 = Point:new(200, 450)
local p2 = Point.create_with_x(100)

print(p1:distance(p2)) -- 460.97722286464

Alternatives

There are some alternatives: