Lesson 9 — Classes
You are here
1–8 done · 9. Classes ←
What are we learning?
A class is a blueprint for a kind of value that has:
- fields — named pieces of data
- methods — functions that belong to that kind of value
An object (or instance) is one value made from that blueprint.
Smallest useful example
echo
class Dog {
new {
name: str;
}
fn bark(this) {
say(this.name, "says woof");
}
}
d: Dog = Dog { name: "Rex" };
d.bark();Output
text
Rex says woofLine by line
class Dog { ... }— define the blueprint namedDog.new { name: str; }— everyDoghas a text field calledname.fn bark(this) { ... }— a method.thismeans “the dog we are talking about.”Dog { name: "Rex" }— create one dog whose name is Rex.d.bark();— runbarkon that dog.
Try it
Create a Cat class with a name field and a meow method that prints the name and says meow. Make one cat and call meow.
Predict it
echo
class Point {
new {
x: int;
y: int;
}
fn show(this) {
say(this.x, this.y);
}
}
p: Point = Point { x: 3, y: 4 };
p.show();Show answer
text
3 4What we are not covering yet
Interfaces, private fields (priv), getters/setters, type methods, and implements are real Echo features. Learn them after you are comfortable with this page: Classes and Interfaces (full).
You finished the Learn path
Next steps:
- Practice — Change, predict, fix
- Small exercises
- Mini projects
- When you need exact rules: Language Reference
- When you already know other languages: Language Tour
