Language
Classes & Objects
Declaring classes, fields, methods, and instances
A class groups fields and methods under a name. Instances are created with new.
Quick reference
class Point
field x
function getX()
return this.x
end
end
p = new Point()
Declaring a class
A class body holds field declarations and function methods, terminated by
end:
class Point
field x
function getX()
return this.x
end
end
field xdeclares an instance field.- A
functioninside a class is a method; it can reach the instance throughthis. this.xreads the fieldxon the current instance.
Creating an instance
Instantiate with new:
p = new Point()
Assign fields with property assignment, and call methods with dotted access:
p.x = 42
print(p.getX())
See also
- Variables & Scope for property assignment (
p.x = 42). - Functions & Closures for the
functionform used as methods. - Java Interop for constructing JVM objects, which also uses call syntax.