140 lines
3.0 KiB
Plaintext
140 lines
3.0 KiB
Plaintext
|
|
class Animal {
|
||
|
|
name = "Unknown";
|
||
|
|
age = 0;
|
||
|
|
_species = "Animal";
|
||
|
|
|
||
|
|
Animal(this, name, age) {
|
||
|
|
this.name = name;
|
||
|
|
this.age = age;
|
||
|
|
print("Animal created:", this.name);
|
||
|
|
}
|
||
|
|
|
||
|
|
~Animal(this) {
|
||
|
|
print("Animal destroyed:", this.name);
|
||
|
|
}
|
||
|
|
|
||
|
|
speak(this) {
|
||
|
|
print(this.name, "makes a sound");
|
||
|
|
}
|
||
|
|
|
||
|
|
getInfo(this) {
|
||
|
|
return this.name + " is " + str(this.age) + " years old";
|
||
|
|
}
|
||
|
|
|
||
|
|
birthday(this) {
|
||
|
|
this.age++;
|
||
|
|
print(this.name, "is now", this.age, "years old");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class Dog {
|
||
|
|
name = "";
|
||
|
|
age = 0;
|
||
|
|
breed = "Unknown";
|
||
|
|
_tricks = null;
|
||
|
|
|
||
|
|
Dog(this, name, age, breed = "Mixed") {
|
||
|
|
this.name = name;
|
||
|
|
this.age = age;
|
||
|
|
this.breed = breed;
|
||
|
|
this._tricks = {};
|
||
|
|
print("Dog created:", this.name, "the", this.breed);
|
||
|
|
}
|
||
|
|
|
||
|
|
speak(this) {
|
||
|
|
print(this.name, "says: Woof!");
|
||
|
|
}
|
||
|
|
|
||
|
|
fetch(this, item) {
|
||
|
|
print(this.name, "fetches the", item);
|
||
|
|
}
|
||
|
|
|
||
|
|
learnTrick(this, trick) {
|
||
|
|
this._tricks.push(trick);
|
||
|
|
print(this.name, "learned:", trick);
|
||
|
|
}
|
||
|
|
|
||
|
|
performTricks(this) {
|
||
|
|
if (len(this._tricks) == 0) {
|
||
|
|
print(this.name, "doesn't know any tricks yet");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
print(this.name + "'s tricks:");
|
||
|
|
for (i = 0; i < len(this._tricks); i++) {
|
||
|
|
print(" -", this._tricks[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class Cat {
|
||
|
|
name = "";
|
||
|
|
lives = 9;
|
||
|
|
__secretThoughts = "I am plotting world domination";
|
||
|
|
|
||
|
|
Cat(this, name) {
|
||
|
|
this.name = name;
|
||
|
|
print("Cat created:", this.name);
|
||
|
|
}
|
||
|
|
|
||
|
|
speak(this) {
|
||
|
|
print(this.name, "says: Meow!");
|
||
|
|
}
|
||
|
|
|
||
|
|
loseLife(this) {
|
||
|
|
if (this.lives > 0) {
|
||
|
|
this.lives--;
|
||
|
|
print(this.name, "lost a life. Lives remaining:", this.lives);
|
||
|
|
} else {
|
||
|
|
print(this.name, "has no more lives!");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
print("=== Class System Demo ===");
|
||
|
|
print("");
|
||
|
|
|
||
|
|
print("--- Creating Animals ---");
|
||
|
|
animal = new Animal("Generic", 5);
|
||
|
|
animal.speak();
|
||
|
|
print(animal.getInfo());
|
||
|
|
animal.birthday();
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Dog Class ---");
|
||
|
|
dog = new Dog("Buddy", 3, "Golden Retriever");
|
||
|
|
dog.speak();
|
||
|
|
dog.fetch("ball");
|
||
|
|
dog.learnTrick("sit");
|
||
|
|
dog.learnTrick("roll over");
|
||
|
|
dog.learnTrick("play dead");
|
||
|
|
dog.performTricks();
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Cat Class ---");
|
||
|
|
cat = new Cat("Whiskers");
|
||
|
|
cat.speak();
|
||
|
|
print("Cat has", cat.lives, "lives");
|
||
|
|
cat.loseLife();
|
||
|
|
cat.loseLife();
|
||
|
|
print("Cat now has", cat.lives, "lives");
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Default Parameters ---");
|
||
|
|
mutt = new Dog("Max", 2);
|
||
|
|
print("Breed:", mutt.breed);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Property Access ---");
|
||
|
|
print("Dog name:", dog.name);
|
||
|
|
print("Dog age:", dog.age);
|
||
|
|
print("Dog breed:", dog.breed);
|
||
|
|
|
||
|
|
dog.age = 4;
|
||
|
|
print("Updated dog age:", dog.age);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Convention-based Privacy ---");
|
||
|
|
print("_species (single underscore):", animal._species);
|
||
|
|
cat.__secretThoughts = "Actually, I just want treats";
|
||
|
|
print("__secretThoughts (double underscore):", cat.__secretThoughts);
|