package com.mrsoft;
public class Cat {
private String name;
private int age;
private double weight;
private String color;
public Cat(String name, int age, double weight, String color) {
this.name = name;
this.age = age;
this.weight = weight;
this.color = color;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Cat cat = (Cat) obj;
return name.equals(cat.name) && (age == cat.age)
&& (weight == cat.weight) && (color.equals(cat.color));
}
public String getInfo() {
StringBuilder sb = new StringBuilder();
sb.append("名字:" + name + " ");
sb.append("年龄:" + age + " ");
sb.append("重量:" + weight + " ");
sb.append("颜色:" + color + " ");
return sb.toString();
}
}
package com.mrsoft;
public class Demo {
public static void main(String[] args) {
Cat cat1 = new Cat("咖啡猫", 12, 21, "黑色");
Cat cat2 = new Cat("波斯猫", 12, 21, "白色");
Cat cat3 = new Cat("咖啡猫", 12, 21, "黑色");
System.out.println("猫咪1号:" + cat1.getInfo());
System.out.println("猫咪2号:" + cat2.getInfo());
System.out.println("猫咪3号:" + cat3.getInfo());
System.out.println("猫咪1号是否与猫咪2号相同:" + cat1.equals(cat2));
System.out.println("猫咪1号是否与猫咪3号相同:" + cat1.equals(cat3));
}
}