Sobes.tech
Junior

Jaka jest różnica między opisem klasy a instancją klasy?

sobes.tech AI

Odpowiedź od AI

Opis klasy to szablon lub schemat do tworzenia obiektów. Określa strukturę (właściwości/pola) i zachowanie (metody) obiektów.

Przykład klasy w JavaScript:

// Opis klasy
class Car {
  constructor(make, model) {
    this.make = make;
    this.model = model;
  }

  displayInfo() {
    console.log(`To jest ${this.make} ${this.model}.`);
  }
}

// Przykłady instancji klasy
const myCar = new Car('Toyota', 'Corolla'); // Instancja 1
const anotherCar = new Car('Honda', 'Civic'); // Instancja 2

// Użycie instancji
myCar.displayInfo(); // Wyświetla "To jest Toyota Corolla."
anotherCar.displayInfo(); // Wyświetla "To jest Honda Civic."