Sfida: Implementare Proprietà e Metodi Statici in una Classe
Compito
Immagina di dover costruire un sistema di gestione dell'inventario per un negozio online. La classe Product deve tenere traccia del numero totale di prodotti aggiunti all'inventario, oltre a fornire una funzionalità per confrontare i prezzi di due prodotti.
- Definire una Proprietà Statica:
- Nella classe
Product, crea una proprietà staticatotalProductsinizializzata a0; - Ogni volta che viene creata una nuova istanza di
Product, incrementatotalProductsdi 1 per tenere traccia di quanti prodotti sono stati aggiunti all'inventario.
- Nella classe
- Definire un Metodo Statico: Definisci un metodo statico
comparePrices(product1, product2)che prende due istanze diProductcome parametri e restituisce:"Product 1 is more expensive"seproduct1ha un prezzo maggiore;"Product 2 is more expensive"seproduct2ha un prezzo maggiore;"Both products have the same price"se hanno lo stesso prezzo.
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
- Definire una proprietà statica chiamata
totalProductse inizializzarla a0; - Nel costruttore, incrementare
Product.totalProductsdi 1 ogni volta che viene creata una nuova istanza diProduct; - Definire un metodo statico
comparePrices(product1, product2)che prende due parametri:product1eproduct2; - In
comparePrices, utilizzare una istruzioneifper verificare seproduct1.priceè maggiore diproduct2.price. Se vero, restituire"Product 1 is more expensive"; - Utilizzare una istruzione
else ifper verificare seproduct1.priceè minore diproduct2.price. Se vero, restituire"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
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
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
Sfida: Implementare Proprietà e Metodi Statici in una Classe
Scorri per mostrare il menu
Compito
Immagina di dover costruire un sistema di gestione dell'inventario per un negozio online. La classe Product deve tenere traccia del numero totale di prodotti aggiunti all'inventario, oltre a fornire una funzionalità per confrontare i prezzi di due prodotti.
- Definire una Proprietà Statica:
- Nella classe
Product, crea una proprietà staticatotalProductsinizializzata a0; - Ogni volta che viene creata una nuova istanza di
Product, incrementatotalProductsdi 1 per tenere traccia di quanti prodotti sono stati aggiunti all'inventario.
- Nella classe
- Definire un Metodo Statico: Definisci un metodo statico
comparePrices(product1, product2)che prende due istanze diProductcome parametri e restituisce:"Product 1 is more expensive"seproduct1ha un prezzo maggiore;"Product 2 is more expensive"seproduct2ha un prezzo maggiore;"Both products have the same price"se hanno lo stesso prezzo.
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
- Definire una proprietà statica chiamata
totalProductse inizializzarla a0; - Nel costruttore, incrementare
Product.totalProductsdi 1 ogni volta che viene creata una nuova istanza diProduct; - Definire un metodo statico
comparePrices(product1, product2)che prende due parametri:product1eproduct2; - In
comparePrices, utilizzare una istruzioneifper verificare seproduct1.priceè maggiore diproduct2.price. Se vero, restituire"Product 1 is more expensive"; - Utilizzare una istruzione
else ifper verificare seproduct1.priceè minore diproduct2.price. Se vero, restituire"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
Grazie per i tuoi commenti!