关于Set接口的对象作为私有属性,这段代码运行结果不明白请讲解?
import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class GoodsManage { // 属性:存放商品的容器(goodsSet):Set类型 private Set<Goods> goodsSet; // 构造方法 public GoodsManage() { } public GoodsManage(Set<Goods> goodsSet) { super(); this.goodsSet = goodsSet; } // getter和setter方法 public Set<Goods> getGoodsSet() { return goodsSet; } public void setGoodsSet(Set<Goods> goodsSet) { this.goodsSet = goodsSet; } // 商品信息导入: public void importGoods() { Goods goods0 = new Goods("goods001", "水杯", 56.0, "不锈钢水杯"); Goods goods1 = new Goods("goods002", "饮水机", 229.0, "带净化功能的饮水机"); Goods goods2 = new Goods("goods003", "笔记本电脑", 4999.0, "15寸笔记本电脑"); Goods goods3 = new Goods("goods004", "手机", 2300.0, "android手机"); goodsSet.add(goods0); goodsSet.add(goods1); goodsSet.add(goods2); goodsSet.add(goods3); } // 显示所有商品信息: public void displayAllGoods() { Iterator<Goods> it = goodsSet.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } public static void main(String[] args) { GoodsManage gm = new GoodsManage(); System.out.println("gm:"+gm.getGoodsSet()); Set<Goods> set1 = new HashSet<Goods>(); GoodsManage gm1 = new GoodsManage(set1); System.out.println("ste1:"+set1); System.out.println("gm1:"+gm1.getGoodsSet()); Set<Goods> set2 = new HashSet<Goods>(); GoodsManage gm2 = new GoodsManage(set2); gm2.importGoods(); System.out.println("ste2:"+set2); System.out.println("gm2:"+gm2.getGoodsSet()); //为什么GoodsManage类的实例化对象gm2执行importGoods成员方法对GoodsManage类私有属性Set<Goods> goodsSet进行修改,Set接口的实现类对象set2会产生改变? } /*运行结果 gm:null ste1:[] gm1:[] ste2:[商品信息[编号:goods004, 名称:手机, 价格:2300.0,描述:android手机], 商品信息[编号:goods002, 名称:饮水机, 价格:229.0,描述:带净化功能的饮水机], 商品信息[编号:goods003, 名称:笔记本电脑, 价格:4999.0,描述:15寸笔记本电脑], 商品信息[编号:goods001, 名称:水杯, 价格:56.0,描述:不锈钢水杯]] gm2:[商品信息[编号:goods004, 名称:手机, 价格:2300.0,描述:android手机], 商品信息[编号:goods002, 名称:饮水机, 价格:229.0,描述:带净化功能的饮水机], 商品信息[编号:goods003, 名称:笔记本电脑, 价格:4999.0,描述:15寸笔记本电脑], 商品信息[编号:goods001, 名称:水杯, 价格:56.0,描述:不锈钢水杯]] */ }
package com.imooc.goods; public class Goods { // 属性:商品名称 (goodsName):String类型,商品价格(price):double类型,商品描述(goodsDesp):String类型 private String goodsId; private String goodsName; private double price; private String goodsDesp; // 构造方法 public Goods() { } public Goods(String goodsId, String goodsName, double price, String goodsDesp) { this.goodsId = goodsId; this.goodsName = goodsName; this.price = price; this.goodsDesp = goodsDesp; } // getter和setter方法 public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this.goodsId = goodsId; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getGoodsDesp() { return goodsDesp; } public void setGoodsDesp(String goodsDesp) { this.goodsDesp = goodsDesp; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((goodsDesp == null) ? 0 : goodsDesp.hashCode()); result = prime * result + ((goodsId == null) ? 0 : goodsId.hashCode()); result = prime * result + ((goodsName == null) ? 0 : goodsName.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj.getClass()==Goods.class) { Goods goods = (Goods) obj; return goods.getGoodsDesp().equals(goodsDesp) && goods.getPrice() == price && goods.getGoodsId().equals(goodsId) && goods.getGoodsName().equals(goodsName); } return false; } // toString()方法 @Override public String toString() { return "商品信息[编号:" + goodsId + ", 名称:" + goodsName + ", 价格:" + price + ",描述:" + goodsDesp + "]"; } }
0
收起
正在回答
1回答
如下代码,创建GoodsManage对象gm2时,set2是方法的参数,然后在GoodsManage的构造方法中,将类的私有属性Set<Goods> goodsSet设置为set2,因此执行importGoods()方法,就是将商品信息添加到set2中,所以会发现set2的值被修改了。
祝学习愉快!
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星