728x90

 

 

처음 배울 때 equals() 내부의 로직이 전혀 이해되지 않았는데

이번에 보면서 대강 왜 이런 비교들을 하는지에 대한 이해가 가기 시작해서 신기했고

그동안 그래도 꽤나 성장했구나를 느꼈다 2,3번 더 하면서 이해의 이해를 더해야겠다!!

 

package chap12_Collection.B_Set.model.vo;

public class Dog {
	
	private String name;
	private double weight;
	
	public Dog() {}
	public Dog(String name, double weight) {
		this.name = name;
		this.weight = weight;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	public String getName() {
		return name;
	}
	public double getWeight() {
		return weight;
	}
	
	@Override
	public String toString() {
		return name + " " + weight+"kg";
	}

	@Override
	public boolean equals(Object obj) {
		
		if(this==obj) {
			return true;
		}
		if(obj==null) {
			return false;
		}
		if(getClass() != obj.getClass()) {
			return false;
		}
		
		Dog other = (Dog)obj;
		if(name == null) {
			if(other.name != null) {
				return false;
			}
		}else if(!name.equals(other.name)) {
			return false;
		}
		
		if(weight != other.weight) {
			return false;
		}
		return true;
		
	}
	
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		
		result = PRIME * result + (name == null ? 0 : name.hashCode());
		result = PRIME * result + (int)weight;
		
		return result;
	}
	
	
}

 

 

 

728x90
반응형

+ Recent posts