Classes
Declaration
To declare a class, use the class keyword:
class Person(name) {
// Property
@name = name
// Method
@greet() {
puts 'Hi, I am ' + @name
}
}This will create a class named Person, whole of class body is the constructor and param name is its parameter.
@name is a property, @greet is a method instance of this class.
There is no "this", just access class members through @.
To create an instance of the Person, just call it:
var p = Person('John')Now, access the members of this instance:
You can also declare a class without param in initializer.
The class name should start with an uppercase character to distinguish it from the function name.
Static members
We do not support to declare static members in class body directly. Fortunately, a class is an object, so you can assign properties for it.
Static properties
Static methods
Inheritance
Create a base class:
Extend superclass:
Override method:
Notes
Like a function, you cannot declare a class in non-top-level of code.
Last updated
Was this helpful?