📋 Interfejsy
Interfejsy definiują kontrakt - jakie metody musi implementować klasa.
Definicja interfejsu
interface Drawable {
fn draw();
fn resize(width, height);
}
Implementacja
class Circle implements Drawable {
init(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
draw() {
print(`Drawing circle at ${this.x}, ${this.y}`);
}
resize(width, height) {
this.radius = min(width, height) / 2;
}
}
class Rectangle implements Drawable {
init(w, h) {
this.width = w;
this.height = h;
}
draw() {
print(`Drawing rect ${this.width}x${this.height}`);
}
resize(width, height) {
this.width = width;
this.height = height;
}
}
let shapes = [Circle(0,0,5), Rectangle(10,20)];
for (shape in shapes) {
shape.draw();
}