Herausforderung: Statische Eigenschaften und Methoden in Einer Klasse Implementieren
Aufgabe
Stellen Sie sich vor, Sie entwickeln ein Inventarverwaltungssystem für einen Online-Shop. Die Klasse Product muss die Gesamtanzahl der zum Inventar hinzugefügten Produkte verfolgen und eine Funktionalität bereitstellen, um die Preise von zwei Produkten zu vergleichen.
- Definieren Sie eine statische Eigenschaft:
- Erstellen Sie in der Klasse
Producteine statische EigenschafttotalProducts, die mit0initialisiert wird; - Jedes Mal, wenn eine neue Instanz von
Producterstellt wird, erhöhen SietotalProductsum 1, um zu verfolgen, wie viele Produkte dem Inventar hinzugefügt wurden.
- Erstellen Sie in der Klasse
- Definieren Sie eine statische Methode: Definieren Sie eine statische Methode
comparePrices(product1, product2), die zwei Instanzen vonProductals Parameter erhält und Folgendes zurückgibt:"Product 1 is more expensive", wennproduct1einen höheren Preis hat;"Product 2 is more expensive", wennproduct2einen höheren Preis hat;"Both products have the same price", wenn beide den gleichen Preis haben.
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
- Definieren Sie eine statische Eigenschaft mit dem Namen
totalProductsund initialisieren Sie sie mit0; - Erhöhen Sie im Konstruktor
Product.totalProductsum 1, jedes Mal, wenn eine neue Instanz vonProducterstellt wird; - Definieren Sie eine statische Methode
comparePrices(product1, product2), die zwei Parameter erhält:product1undproduct2; - Verwenden Sie in
comparePriceseineif-Anweisung, um zu prüfen, obproduct1.pricegrößer alsproduct2.priceist. Falls ja, geben Sie"Product 1 is more expensive"zurück; - Verwenden Sie eine
else if-Anweisung, um zu prüfen, obproduct1.pricekleiner alsproduct2.priceist. Falls ja, geben Sie"Product 2 is more expensive"zurück.
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
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you explain how static properties and methods work in JavaScript?
What would happen if I created more Product instances?
Can you show how to add more features to the Product class?
Awesome!
Completion rate improved to 2.22
Herausforderung: Statische Eigenschaften und Methoden in Einer Klasse Implementieren
Swipe um das Menü anzuzeigen
Aufgabe
Stellen Sie sich vor, Sie entwickeln ein Inventarverwaltungssystem für einen Online-Shop. Die Klasse Product muss die Gesamtanzahl der zum Inventar hinzugefügten Produkte verfolgen und eine Funktionalität bereitstellen, um die Preise von zwei Produkten zu vergleichen.
- Definieren Sie eine statische Eigenschaft:
- Erstellen Sie in der Klasse
Producteine statische EigenschafttotalProducts, die mit0initialisiert wird; - Jedes Mal, wenn eine neue Instanz von
Producterstellt wird, erhöhen SietotalProductsum 1, um zu verfolgen, wie viele Produkte dem Inventar hinzugefügt wurden.
- Erstellen Sie in der Klasse
- Definieren Sie eine statische Methode: Definieren Sie eine statische Methode
comparePrices(product1, product2), die zwei Instanzen vonProductals Parameter erhält und Folgendes zurückgibt:"Product 1 is more expensive", wennproduct1einen höheren Preis hat;"Product 2 is more expensive", wennproduct2einen höheren Preis hat;"Both products have the same price", wenn beide den gleichen Preis haben.
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
- Definieren Sie eine statische Eigenschaft mit dem Namen
totalProductsund initialisieren Sie sie mit0; - Erhöhen Sie im Konstruktor
Product.totalProductsum 1, jedes Mal, wenn eine neue Instanz vonProducterstellt wird; - Definieren Sie eine statische Methode
comparePrices(product1, product2), die zwei Parameter erhält:product1undproduct2; - Verwenden Sie in
comparePriceseineif-Anweisung, um zu prüfen, obproduct1.pricegrößer alsproduct2.priceist. Falls ja, geben Sie"Product 1 is more expensive"zurück; - Verwenden Sie eine
else if-Anweisung, um zu prüfen, obproduct1.pricekleiner alsproduct2.priceist. Falls ja, geben Sie"Product 2 is more expensive"zurück.
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
Danke für Ihr Feedback!