JAVA重写equals方法

public class new4 {
    //学生ID
    public String id;
    //学生姓名
    public String name;

    public new4(String idCard, String name) {
        this.id = idCard;
        this.name = name;
    }

    //重写equals方法
    public boolean equals(Object obj) {
        new4 e = (new4) obj;
        if (id == null) {
            if (e.id != null)
                return false;
        } else if (!id.equals(e.id))
            return false;
        if (name == null) {
            if (e.name != null)
                return false;
        } else if (!name.equals(e.name))
            return false;
        return true;
    }

    public static void main(String[] args) {
        new4 p1 = new new4("1001", "我帅");
        new4 p2 = new new4("1001", "我丑");
        System.out.println(p1.equals(p2));
    }
}

 

posted @ 2017-10-23 18:57  ichimoku  阅读(3220)  评论(0编辑  收藏  举报