java对象常用方法(Common methods of Java objects)

一、hashCode,equals
两个对象的equals值相等的话,hashcode必须相等
equals是逻辑比较,==是物理比较
二、toString方法
@Override
public String toString() {
return “test{” +
“name='” + name + ”’ +
“, age=” + age +
‘}’;
}
三、对象克隆的要求
1、实现Cloneable 接口
2、实现clone方法
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}

————————

1、 Hashcode, equals
If the equals values of two objects are equal, hashcode must be equal
Equals is a logical comparison and = = is a physical comparison
2、 ToString method
@Override
public String toString() {
return “test{” +
“name='” + name + ”’ +
“, age=” + age +
‘}’;
}
3、 Requirements for object cloning
1. Implement clonable interface
2. Implement clone method
@Override
public Object clone() throws CloneNotSupportedException {
return super. clone();
}