チャレンジ:クラスにおける静的プロパティとメソッドの実装
メニューを表示するにはスワイプしてください
課題
オンラインストアの在庫管理システムを構築することを想定します。Product クラスは在庫に追加された商品の総数を追跡し、2つの商品の価格を比較する機能を提供する必要があります。
- 静的プロパティの定義:
Productクラス内で静的プロパティtotalProductsを0で初期化;- 新しい
Productインスタンスが作成されるたびにtotalProductsを1増やし、在庫に追加された商品の数を追跡する。
- 静的メソッドの定義: 静的メソッド
comparePrices(product1, product2)を定義し、2つのProductインスタンスを引数に受け取って、以下を返す:"Product 1 is more expensive":product1の価格がより高い場合;"Product 2 is more expensive":product2の価格がより高い場合;"Both products have the same price": 価格が同じ場合。
12345678910111213141516171819202122232425262728293031class Product { _____ _____ = 0; // Define static property for total products constructor(name, price) { this.name = name; this.price = price; Product._____+=1; // Increment totalProducts } // Static method to compare prices _____ comparePrices(product1, product2) { if (product1.price > product2.price) { return _____; } else if (_____ < _____) { return _____; } else { return 'Both products have the same price'; } } } // Testing const product1 = new Product('Laptop', 1200); const product2 = new Product('Smartphone', 800); const product3 = new Product('Tablet', 1200); console.log(Product.comparePrices(product1, product2)); // Expected: Product 1 is more expensive console.log(Product.comparePrices(product1, product3)); // Expected: Both products have the same price console.log(Product.comparePrices(product2, product3)); // Expected: Product 2 is more expensive console.log(Product.totalProducts); // Expected: 3
totalProductsという名前の静的プロパティを定義し、0で初期化;- コンストラクタ内で、新しい
Product.totalProductsインスタンスが作成されるたびにProductを1増やす; - 静的メソッド
comparePrices(product1, product2)を定義し、2つのパラメータproduct1とproduct2を受け取る; comparePrices内で、if文を使ってproduct1.priceがproduct2.priceより大きいかを判定し、該当する場合は"Product 1 is more expensive"を返す;else if文を使ってproduct1.priceがproduct2.priceより小さいかを判定し、該当する場合は"Product 2 is more expensive"を返す。
12345678910111213141516171819202122232425262728293031class Product { static totalProducts = 0; // Define static property for total products constructor(name, price) { this.name = name; this.price = price; Product.totalProducts+=1; // Increment totalProducts } // Static method to compare prices static comparePrices(product1, product2) { if (product1.price > product2.price) { return 'Product 1 is more expensive'; } else if (product1.price < product2.price) { return 'Product 2 is more expensive'; } else { return 'Both products have the same price'; } } } // Testing const product1 = new Product('Laptop', 1200); const product2 = new Product('Smartphone', 800); const product3 = new Product('Tablet', 1200); console.log(Product.comparePrices(product1, product2)); // Output: Product 1 is more expensive console.log(Product.comparePrices(product1, product3)); // Output: Both products have the same price console.log(Product.comparePrices(product2, product3)); // Output: Product 2 is more expensive console.log(Product.totalProducts); // Output: 3
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 13
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 1. 章 13