关于JAVA购物车,有一句代码一点都没看懂
package org.jndx.util;
import java.util.HashMap;
import java.util.Map;
import org.jndx.pojo.Product;
public class ShoppingCart {
private Map map=new HashMap();
public void addProducts(Product p,int num){
if(map.containsKey(p.getId())){
Items items=(Items)map.get(p.getId());//就是这句话,到底是什么意思啊?这样子为什么就能让
//items指向指定的商品?然后在指定的商品上加上数量?
//为什么要强行转换为Items类呢?
items.addNums(num);
}else{
map.put(p.getId(), new Items(p,num));
}
}
public void delProduct(Product p,int num){
//map.remove(key)
}
public void print(){
/*
*
* --------------------------
* 1 mianbao 4 2.5 10
* 2
* 3-------------------------------
*
*
*
*
*/
}
}
package org.jndx.util;
import org.jndx.pojo.Product;
public class Items {
private Product p;
private int num;
public Product getP() {
return p;
}
public void setP(Product p) {
this.p = p;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Items(Product p, int num) {
super();
this.p = p;
this.num = num;
}
public void addNums(int num){
this.num+=num;
}
}