Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ チャレンジ:JavaScriptクラスを作成する | JavaScriptクラスと継承の習得
JavaScriptロジックとインタラクション

bookチャレンジ:JavaScriptクラスを作成する

メニューを表示するにはスワイプしてください

課題

車両レンタルフリートを管理するシステムの作成。各車両には、メーカー、モデル、年式の詳細がある。これらの詳細を表現するための Vehicle クラスの作成。

  1. クラスの作成: Vehicle という名前のクラスを定義;
  2. コンストラクタの追加: Vehicle クラス内で:
    • 3つのパラメータ makemodelyear を受け取るコンストラクタの定義;
    • これらのパラメータをクラスのプロパティに割り当てる。
  3. インスタンスの作成とテスト
    • car1 という名前の Vehicle インスタンスを "Toyota""Camry"2020 の値で作成;
    • car2 という別のインスタンスを "Ford""Mustang"2018 の値で作成;
    • 各車インスタンスのプロパティをログ出力する。
1234567891011121314151617181920
class _____ { constructor(_____, _____, _____) { this._____ = _____; this._____ = _____; this._____ = _____; } } // Create instances const car1 = new _____(_____, _____, _____); const car2 = new _____(_____, _____, _____); // Output the details console.log(car1._____); // Expected: Toyota console.log(car1._____); // Expected: Camry console.log(car1._____); // Expected: 2020 console.log(car2._____); // Expected: Ford console.log(car2._____); // Expected: Mustang console.log(car2._____); // Expected: 2018
copy
  • Vehicle という名前のクラスを定義;
  • 3つのパラメータ makemodelyear を持つコンストラクタを追加;
  • コンストラクタ内で、各パラメータを this を使ってプロパティに割り当てる;
  • Vehicle クラスのインスタンス car1"Toyota""Camry"2020 の値で作成;
  • Vehicle クラスの別のインスタンス car2"Ford""Mustang"2018 の値で作成;
  • console.log() を使って car1car2 のプロパティを表示。
1234567891011121314151617181920
class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } } // Create instances const car1 = new Vehicle('Toyota', 'Camry', 2020); const car2 = new Vehicle('Ford', 'Mustang', 2018); // Output the details console.log(car1.make); // Output: Toyota console.log(car1.model); // Output: Camry console.log(car1.year); // Output: 2020 console.log(car2.make); // Output: Ford console.log(car2.model); // Output: Mustang console.log(car2.year); // Output: 2018
copy

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  3
some-alt