Object-Oriented Programming
This guide introduces the OOP model used in Eiger, including Classes and Dataclasses.
Classes
A class defines a blueprint for objects. It can contain properties and functions (methods), and it supports constructors for initializing instances.
Syntax
class ClassName {
let [modifiers] propertyName
~ The `new` function acts as a constructor.
func new(parameters) {
~ `this` is a reference to the current class instance.
this.propertyName = parameter
}
}
Example
class Person {
let private m_name
let private m_age
let private m_birthYear
~ Constructor
func new(name, age, birthYear) {
this.m_name = name
this.m_age = age
this.m_birthYear = birthYear
}
~ Method
func summary() {
ret (m_name + " (" + m_age.asString + " years old, born " + m_birthYear.asString + ")")
}
}
~ Creating a class instance from a "blueprint" (class)
let personOne = Person("John", 25, 2000)
~ Calling the summary method in personOne
emitln(personOne.summary())
Features
- Properties are declared with
let
, just like variables - Access Modifiers:
public
(default): Accessible from anywhereprivate
: Accessible only within the class
- Constructor:
- Declared with
func new(params)
- Used to initialize the object
- Declared with