Classes and Objects

A class defines the attributes (member variables), constructors, and behaviors (methods) of objects.

Defining a Class

Syntax

class ClassName{
    attribute1 :: dataType
    attribute2 :: dataType
    
    // Define class
    def ClassName(parameter1, parameter2){
        attribute1 = parameter1
        attribute2 = parameter2
    }
    
    // Define method
    def method1(parameter){        
    }
}
Note:
  • Class names cannot start with a number, contain spaces or any special characters except underscores (_).

  • Parameters cannot have the same name as attributes, otherwise attributes will be overwritten.

  • JIT and destructors are not supported currently.

For example, define a class Person:

class Person {
  name :: STRING
  age :: INT
  
  def Person(name_, age_) {
    name = name_
    age = age_
  }
  
  def setName(newName) {
    name = newName
  }
  
  def getName() {
    return name
  }
}

Instantiating an Object

A class is a template for objects, and an object is an instance of class.

The following example instantiates an object of class Student:
a = Person("Harrison",20)