inheritance

This commit is contained in:
Andrea Bianchi
2026-04-29 09:54:15 +09:00
parent e8fb1810e3
commit fec15709e7
9 changed files with 110 additions and 0 deletions

43
w9_cars/sketch.js Normal file
View File

@@ -0,0 +1,43 @@
const vehicles = [];
const IMG_SIZE = 400;
let message = '';
const images = {};
function setup() {
createCanvas(4 * IMG_SIZE, 300);
vehicles.push(new Car(4));
}
function draw() {
background(255);
for (let i = 0; i < vehicles.length; i += 1) {
vehicles[i].draw(i * IMG_SIZE, 0, IMG_SIZE);
}
fill(0);
textFont('Arial', 20);
textAlign(CENTER);
text(message, width / 2, IMG_SIZE);
}
function mousePressed() {
const index = floor(mouseX / IMG_SIZE);
message = vehicles[index]?.getDescription() ?? 'None'; // polymorphism!
}
// CLASSES
class Car {
constructor(passengers) {
// ...
}
getDescription() {
// PLACEHOLDER
}
draw() {}
}