故事的開頭是這樣子的…
在很久很久以前,有一個學生叫小明
有一天,老師對小明,小明啊,你去給班上買一個籃球,一個足球,一套積木和一套樂高
class Good {
constructor(name, price) {
this.name = name;
this.price = price;
}
showInfo() {
console.log(`商品名:${this.name} 價格:¥${this.price}`);
}
}
class Shop {
constructor(customer) {
this.customer = customer;
}
sell(good) {
this.customer.buy(good);
this.customer.total += good.price;
}
}
class ToyShop extends Shop{
constructor(customer) {
super(customer);
}
sell(name) {
switch(name) {
case '樂高': super.sell(new Good('樂高', 3600));
case '積木': super.sell(new Good('積木', 288));
}
}
}
class SportShop extends Shop{
constructor(customer) {
super(customer);
}
sell(name) {
switch(name) {
case '足球': super.sell(new Good('足球', 80));
case '籃球': super.sell(new Good('籃球', 65));
}
}
}
class Student {
constructor(name) {
this.name = name;
this.goodList = [];
this.total = 0;
}
buy(good) {
this.goodList.push(good);
}
getTotal() {
console.log(`${this.name}一共花費了¥${this.total}`);
}
}
於是,小明拿着錢,走在大街上…
let xiaoming = new Student('小明');
小明進了球具店,買了足球和籃球
let sportShop = new SportShop(xiaoming);
sportShop.sell('足球');
sportShop.sell('籃球');
小明進了文具店,買了樂高和積木
let toyShop = new ToyShop(xiaoming);
toyShop.sell('樂高');
toyShop.sell('積木');
小明結束了採購
xiaoming.getTotal();
小明並不關心每次購買的過程是怎樣的:商家庫存少了一個商品,商家盈利了多少錢,小明賬單上多了多少錢
小明只關注一件事情:商家賣出了一件商品給我