testequals.java

来自「常用的java控件使用实例源代码」· Java 代码 · 共 34 行

JAVA
34
字号

public class TestEquals {
	public static void main(String[] agrs) {
		Cat c1 = new Cat(1,2,3);
		Cat c2 = new Cat(1,2,3);
		System.out.println(c1 == c2);
		System.out.println(c1.equals(c2));
	}
}

class Cat {
	int color;
	int height,weight;
	
	public Cat(int color, int height, int weight) {
		this.color = color;
		this.height = height;
		this.weight = weight;
	}
	
	public boolean equals (Object obj) {
		if (obj == null ) return false;
		else {
			if(obj instanceof Cat) {
				Cat c = (Cat)obj;
				if(c.color == this.color && c.height == this.height && c.weight == this.weight) {
					return true;
				}
			}
		}
		return false;
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?