public void setShopList(ShopList shopList) { this.shopList = shopList; }
时间: 2023-08-20 17:04:18 浏览: 30
这是 Java 中 Customer 类的一部分代码。其中,setShopList 方法用于设置顾客的购物清单,接受一个 ShopList 类型的参数 shopList,表示要设置的购物清单。该方法没有返回值。它通过访问关键字 this 来设置私有变量 shopList,将传入的参数值赋给该变量,完成购物清单的设置。由于传入的是一个对象参数,因此该方法可以直接修改顾客的购物清单内容。
相关问题
public class Customer extends Person { /** * @param name * @param idCode * @param telephoneNum * @param address * @param bankCardId */ public Customer(String name, String idCode, String telephoneNum, String address, String bankCardId) { super(name, idCode, telephoneNum, address); this.bankCardId = bankCardId; } private String bankCardId; private ShopList shopList = new ShopList(); public boolean pay(int price) { return false; } public boolean addCommentToFood(String comment) { return false; } /** * @return the bankCardId */ public String getBankCardId() { return bankCardId; } /** * @return the shopList */ public ShopList getShopList() { return shopList; } /** * @param bankCardId the bankCardId to set */ public void setBankCardId(String bankCardId) { this.bankCardId = bankCardId; } /** * @param shopList the shopList to set */ public void setShopList(ShopList shopList) { this.shopList = shopList; } /* * (non-Javadoc) * * @see Person#toString() */ @Override public String toString() { // TODO Auto-generated method stub return super.toString() + "BankCardId: " + getBankCardId(); } }
这是一个 Java 类,名为 `Customer`,继承了 `Person` 类。该类有一个构造方法,接受五个参数,并将其中四个参数传递给父类的构造方法,初始化了 `bankCardId` 成员变量。该类还有一个 `ShopList` 类型的成员变量 `shopList`,并提供了访问和修改该成员变量的方法。该类还有两个方法 `pay` 和 `addCommentToFood`,但这两个方法的实现是返回了 false。最后,该类重写了 `toString` 方法,返回了一个字符串,包含了父类的 `toString` 方法返回的字符串以及 `bankCardId` 的值。
public ShopList() { }
这段代码是Java类ShopList的构造函数的实现。构造函数是Java类中的一个特殊方法,用于在创建对象时初始化对象的成员变量。
在该构造函数中,没有进行任何初始化操作,即该构造函数不接受任何参数,也没有定义任何成员变量。因此,当创建一个ShopList对象时,会调用该构造函数,但是不会进行任何初始化操作,只是创建了一个空对象。
在Java中,每个类都有一个默认的构造函数,如果没有显式定义构造函数,则使用默认的构造函数。默认的构造函数不接受任何参数,也不进行任何初始化操作。如果需要进行初始化操作,则需要显式定义一个构造函数,并在其中实现初始化操作。
相关推荐
![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)
![](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)
try { double total = 0; for (String[] info : cart.values()) { String SQL = "SELECT * FROM shoplist where id = " + info[0]; con = DriverManager.getConnection(url, user, password); sql = con.createStatement(); rs = sql.executeQuery(SQL); // 显示商品信息 while(rs.next()){ String name = rs.getString(2); double price = rs.getDouble(3); int qxy = Integer.parseInt(info[1]); double subtotal = price * qxy; total += subtotal; out.print(""); out.print("" + name + " " + price + "元 " + qxy + " " + subtotal + "元 "); out.print("删除 "); out.print(" ");这段代码在购物城中如何实现数量的增减
![](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)