/*
* 定义一个Animal类
成员变量:
int age,String name,double price
手写它的equals方法,比较getClass和instanceof的区别
*
*
* */
public class Animal {
public static void main(String[] args) {
Dog s1=new Dog(1,26,8);
Dog s2 = new Dog(1,27,8);
System.out.println(s1.equals(s2));
}
}
class Dog{
int age;
String name;
double price;
public Dog() {
}
public Dog(int age, String name, double price) {
this.age=age;
this.name=name;
this.price=price;
}
@Override
public boolean equals(Object obj) {
//1.满足自反性,自己和自己比,
if (this == obj) return true;
//2.排他性
//1.getClass
if (obj == null || this.getClass() != obj.getClass()) return false;
//2.2 instance of
if (!(obj instanceof Dog)) return false;
}
