🏗️ 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());
person.birthday();
print(person.age);
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());
Visibility modifiers
class BankAccount {
init(balance) {
this.balance = balance;
}
getBalance() {
return this.balance;
}
private validateAmount(amount) {
return amount > 0;
}
withdraw(amount) {
if (this.validateAmount(amount)) {
this.balance = this.balance - amount;
}
}
}
let account = BankAccount(1000);
account.getBalance();
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());