Object oriented
Classes
A file can grow a type of its own.
Fields remember. Methods do the work. self is the object talking to itself, which is still a little embarrassing.
Split the class into another file if you like — import, public, and private still keep the hallway polite.
class
define class opens a type. The name is the type. Close it with end;, like a function that learned to sit still.
public:
define class Person;
private:
define str name is "Unknown";
define num age is 0;
public:
define constructor(str name, num age);
redefine self.name is name;
redefine self.age is age;
end;
end;
Fields
Typed define lines inside the class. Usually private, because nobody asked to rummage through your pockets.
Give them a starter value, or fill them in the constructor.
constructor
define constructor runs when someone says
newobj.
Parameters are typed. Write them onto the object with self.
define constructor(str name, num age);
redefine self.name is name;
redefine self.age is age;
end;
Methods
Functions that live on the class. Call them on an object:
call jacob.Birthday();
From inside, reach another method with call self.FormatName();.
define func Birthday();
redefine self.age is self.age + 1;
end;
define func GetAge() returning num;
return self.age;
end;
self
The current object.
self.field reads a field.
redefine self.field is ...; writes one.
Callers never say self. They have a name for the object. self is an inside voice.
newobj
Build an instance. The type is the class name. The arguments go to the constructor.
This is not new.
new is an empty chair. newobj is inviting someone.
You may later redefine that variable with another object of a compatible type. Same chair, new guest.
define Person jacob is newobj Person("Jacob", 21);
define Person alice is newobj Person("Alice", 30);
call jacob.Birthday();
showln call jacob.GetName();
showln call jacob.GetAge();
Full two-file walkthrough:
Classes using self, fields, and methods.
Inheritance — extends, parent, override, abstract — lives under
Inheritance.