🏗️ Klasy

Klasy pozwalajÄ… na programowanie obiektowe w KitsuneScript.

Podstawowa klasa

class Person { init(name, age) { this.name = name; this.age = age; } greet() { return `Cześć, jestem ${this.name}!`; } birthday() { this.age = this.age + 1; } } let person = Person("Jan", 25); print(person.greet()); // "Cześć, jestem Jan!" person.birthday(); print(person.age); // 26

Konstruktor (init)

class Rectangle { init(width, height) { this.width = width; this.height = height; } area() { return this.width * this.height; } } let rect = Rectangle(10, 5); print(rect.area()); // 50

Visibility modifiers

class BankAccount { init(balance) { this.balance = balance; } // Publiczna (domyślnie) getBalance() { return this.balance; } // Prywatna private validateAmount(amount) { return amount > 0; } withdraw(amount) { if (this.validateAmount(amount)) { this.balance = this.balance - amount; } } } let account = BankAccount(1000); account.getBalance(); // OK // account.validateAmount(50); // Error! Private

Companion object

class Counter { init() { this.value = Counter.defaultValue; } increment() { this.value++; Counter.totalIncrements++; } companion { let defaultValue = 0; let totalIncrements = 0; fn getTotalIncrements() { return Counter.totalIncrements; } } } let c1 = Counter(); let c2 = Counter(); c1.increment(); c2.increment(); print(Counter.getTotalIncrements()); // 2