日期:2014-05-20 浏览次数:20710 次
public class Cat { /** * @author */ private String color; private int height; private int weight; Cat(String color,int height,int weight){ this.color=color; this.height=height; this.weight=weight; } public static String Deng(Cat a,Cat b){ if(a==b){ return "C1==C2"; }else { return "C1!=C2"; } } public static String EquMao(Cat c,Cat d){ if(c.equals(d)){ return "C1 Equels C2"; }else{ return "C1 is not Equels C2"; } } public static void main(String[] args) { // TODO Auto-generated method stub Cat c1=new Cat("Blue",5,6); Cat c2=new Cat("Blue",5,6); Deng(c1, c2); EquMao(c1, c1); } }
public class Cat { /** * @author */ private String color; private int height; private int weight; Cat(String color, int height, int weight) { this.color = color; this.height = height; this.weight = weight; } public static String Deng(Cat a, Cat b) { if (a == b) { return "C1==C2"; } else { return "C1!=C2"; } } public static String EquMao(Cat c, Cat d) { if (c.equals(d)) { return "C1 Equals C2"; } else { return "C1 is not Equals C2"; } } @Override public boolean equals(Object obj) { if (this == obj) {//同一对象直接返回true return true; } if (obj instanceof Cat) {//是不是 Cat 的实例,如果是,强转类型 Cat cat = (Cat) obj; if (cat.color.equals(this.color) && cat.height == this.height && cat.weight == this.height) {//属性相同,则为equals return true; } } return false; } public static void main(String[] args) { // TODO Auto-generated method stub Cat c1 = new Cat("Blue", 5, 6); Cat c2 = new Cat("Blue", 5, 6); System.out.println(Deng(c1, c2)); System.out.println(EquMao(c1, c1)); } }