概述
展开全部
首先先确定到底是要判断62616964757a686964616fe59b9ee7ad9431333363373764是同一个对象还是两个对象里面的内容一毛一样,若是要判断是否是同一个对象直接用“==”就好了,若是判断内容是否一毛一样package com.havefun.play;
public class ObjectJudge {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public ObjectJudge(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
// 重写hashcode
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
// 重写equals
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ObjectJudge other = (ObjectJudge) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public static void main(String[] args) {
ObjectJudge person = new ObjectJudge("aaa", 11);
ObjectJudge person2 = new ObjectJudge("aaa", 11);
System.out.println(person == person2);
System.out.println(person.equals(person2));
}
}
最后
以上就是拼搏季节为你收集整理的java比较对象_java怎么比较两个对象是否相等?的全部内容,希望文章能够帮你解决java比较对象_java怎么比较两个对象是否相等?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复