JavaScript定义商品类有属性:商品ID,商品名称name,商品价格price,商品数量num。有获取商品总价的方法getTPrice。并创建3种以上的商品,进行测试。代码怎么写
时间: 2024-06-03 12:07:50 浏览: 127
利用javascript技术计算商品总价
5星 · 资源好评率100%
class Goods {
constructor(goodsId, name, price, num) {
this.goodsId = goodsId;
this.name = name;
this.price = price;
this.num = num;
}
getTPrice() {
return this.price * this.num;
}
}
let goods1 = new Goods(1, 'Apple', 2.5, 10);
let goods2 = new Goods(2, 'Banana', 1.5, 20);
let goods3 = new Goods(3, 'Orange', 3.0, 15);
console.log(goods1.getTPrice()); // 25
console.log(goods2.getTPrice()); // 30
console.log(goods3.getTPrice()); // 45
阅读全文