package edu.mama.ls16.homework;

    public class Commodity {
    private String ID;
    private String name; //商品名称
    private String model; //商品型号

    1. 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 /> }
    2. public String getID() {<br /> return ID;<br /> }
    3. public String getName() {<br /> return name;<br /> }
    4. public String getModel() {<br /> return model;<br /> }
    5. public void setName(String name) {<br /> this.name = name;<br /> }
    6. public void setModel(String model) {<br /> this.model = model;<br /> }
    7. //以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 /> }
    8. //使用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 /> }
    9. @Override<br /> public int hashCode() {<br /> return this.ID.hashCode();<br /> }<br />}