package com.te.jdkapi;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.Objects;
/*
学习equals的方法
*/
public class Study_Equels {
public static void main(String[] args) {
Animal a1 = new Animal();
Animal a2 = new Animal();
System.out.println(a1==a2);
}
}
class Animal {
private String name;
private int age;
@Override
public boolean equals(Object o) {
if (o instanceof Animal){ // 返回True,当前比较两个对象类型相同
// 比较两个对象的属性值是否相同
Animal st = (Animal) o; // 将传递过来的Object类向下强转为Animal类
if (this.name == st.name && this.age == st.age){
return true;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}