public class ShoppingCar { private double totalPrice; private int totalCount; private Map<Integer, OrderItemBean> itemMap; 为什么map初始化为空 public ShoppingCar() { } public ShoppingCar(double totalPrice, int totalCount, Map<Integer, OrderItemBean> itemMap) { this.totalPrice = 0; this.totalCount = 0; this.itemMap = new HashMap<>(); }
时间: 2024-04-07 19:34:01 浏览: 17
在你提供的代码中,构造函数`ShoppingCar(double totalPrice, int totalCount, Map<Integer, OrderItemBean> itemMap)`中的map初始化为空的原因是,在构造函数中,你重写了初始化map的代码。具体来说,你在构造函数中使用了以下代码:
```
this.itemMap = new HashMap<>();
```
这行代码会创建一个新的空的HashMap对象,并将其赋值给`itemMap`属性。但是,请注意,这行代码会在每次使用该构造函数创建对象时执行,而不是在类的默认构造函数中执行。
因此,如果你在创建`ShoppingCar`对象时使用了带参数的构造函数,并传入了`totalPrice`、`totalCount`和`itemMap`参数,那么map就会被重新初始化为空的HashMap对象。
如果你希望在使用带参数构造函数创建对象时保留原有的map对象,你可以修改构造函数的逻辑,如下所示:
```java
public ShoppingCar(double totalPrice, int totalCount, Map<Integer, OrderItemBean> itemMap) {
this.totalPrice = totalPrice;
this.totalCount = totalCount;
this.itemMap = itemMap;
}
```
这样,当你使用带参数构造函数创建对象时,传入的map对象就会被赋值给`itemMap`属性,而不会重新初始化为空的HashMap对象。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
讲解段以下代码 package com.qst.chapter04.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/ShoppingListServlet") public class ShoppingListServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //do get被动 do post主动 HttpSession session = request.getSession(); //服务器会话对象 HttpSession类 通过request的getSession方法获取(boolean值) //通过request对象获取请求参数 Map<String, Integer> car = (Map<String, Integer>) session .getAttribute("shoppingCar"); if (car != null && car.size() > 0) { out.println("您购买的书籍有:"); // 遍历显示购物车中的书籍名称和选择次数 for (String bookName : car.keySet()) { out.println("" + bookName + " , " + car.get(bookName) + " 本"); } } else { out.println("您还未购买任何书籍!"); } out.println("继续购买"); } }
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)