package edu.mama.ls16.homework;
public class Commodity {
private String ID;
private String name; //商品名称
private String model; //商品型号
public Commodity(String ID, String name, String model) {<br /> this.ID = ID;<br /> this.name = name;<br /> this.model = model;<br /> }<br /> public Commodity(String ID, String name) {<br /> this.ID = ID;<br /> this.name = name;<br /> this.model = null;<br /> }
public String getID() {<br /> return ID;<br /> }
public String getName() {<br /> return name;<br /> }
public String getModel() {<br /> return model;<br /> }
public void setName(String name) {<br /> this.name = name;<br /> }
public void setModel(String model) {<br /> this.model = model;<br /> }
//以sep为分隔符,将Commodity内容拼接成字符串<br /> public String toString(String sep) {<br /> StringBuilder sb=new StringBuilder();<br /> sb.append(this.ID).append(sep).append(this.name).append(sep).append(this.model);<br /> return sb.toString();<br /> }
//使用Map,重写equals和hasCode方法<br /> @Override<br /> public boolean equals(Object obj) {<br /> if(this==obj)<br /> return true; //地址相等<br /> if(obj instanceof Commodity){<br /> Commodity other=(Commodity)obj;<br /> if(this.ID.equals(other.ID))<br /> return true; //ID相同<br /> }<br /> return false;<br /> }
@Override<br /> public int hashCode() {<br /> return this.ID.hashCode();<br /> }<br />}