Object类equals()

public class ObjectDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Demo d1 = new Demo(4);
		Demo d2 = new Demo(45);
		Demo d3 = d1;
		System.out.println(d1.equals(d2));
		System.out.println(d1.equals(d3));
		
		Person p1 = new Person();
		Person p2 = new Person();
		
		System.out.println(p1.equals(p2));
		
		System.out.println(d1.equals(p1));
	}

}

class Demo{
	private int num;
	Demo(int num){
		this.num = num;
	}
	public boolean equals(Object obj) {
		
		if (obj instanceof Demo) {
			Demo demo = (Demo)obj;
			return this.num == demo.num;
		}
		
		return false;
	}
}

class Person{
	
}

你可能感兴趣的:(Object类equals())