重写equal
程序员文章站
2022-05-20 12:20:49
...
今天有点空写了一段基础代码复习一下,随便记记:
package com.dao; public class OverwriteEquls { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } //重写equals方法,当id和那么相同时我们认为两个对象是相同的 public boolean equals(Object o){ OverwriteEquls o3 = (OverwriteEquls) o; boolean b=false; if(this.id==o3.getId() && this.name.equals(o3.getName())){ b=true; } return b; } public static void main(String[] args) { OverwriteEquls o1= new OverwriteEquls(); OverwriteEquls o2 = new OverwriteEquls(); o1.setId(12l); o1.setName("LIMEI"); o2.setId(12l); o2.setName("LIMEI"); System.out.println(o1==o2); System.out.println(o1.equals(o2)); } }