Object类是所有类的直接或间接父类;
在java应用程序中,如果一个类没有使用extends关键字继承指定父类,那么该类默认继承Object类;
toString():默认返回当前对象的字符串表示;
//类的全名称+@+hashcode
hashcode:返回当前对象的hash值(整数)
hash:
相同内容的hash值必然相同;
相同的hash值的内容不一定相同(概率很小);
eg:5+5=10; 2+8=10; 3+7=10: 4+6=10;
————hash碰撞
equales重写
package code62607;
public class TestEquals {
public static void main(String[] args) {
Dog d = new Dog("小黄",3,30);
Dog d1 = new Dog("小黄", 3, 30);
boolean flag=d==d1;//比较的是引用地址;
System.out.println(flag);
flag=d.equals(d1);
System.out.println(flag);
String s="abc";
String s2="abc";
System.out.println(s.equals(s2));
}
}
class Dog
{
private String name;
private int age;
private int weight;
public Dog() {
}
public Dog(String name, int age, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
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;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public boolean equals(Object obj)
{
if(obj==null)
{
return false;
}
if(this==obj)
{
return true;
}
if(obj instanceof Dog)
{
Dog dog=(Dog)obj ;
if(this.name.equals(dog.name)&&this.age==dog.age&&this.weight==dog.weight){
return true;
}
}
return false;
}
}