글쎄, 내 코드는 작동하지만이 경우에는 private
변수가 작동하는 이유를 이해할 수 없다. equals()
.Comparable Class equals 함수는 private 변수에서 작동합니까?
또는 동일한 종류의 객체 구조 안의 다른 객체를 호출하면 private
식별자가 포함되지 않습니다.
public class TestClass implements Comparable <TestClass> {
private final String name;
public TestClass(String name) {
this.name = name;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof TestClass) {
return ((TestClass) obj).name.equals(name); //<- how does this work, isn't name private?
} else {
return false;
}
}
@Override
public int compareTo(TestClass test) {
int thisValue = hashCode();
int otherValue = test.hashCode();
if (thisValue < otherValue) {
return -1;
} else if (thisValue > otherValue) {
return 1;
} else {
return 0;
}
}
@Override
public String toString() {
return name;
}
}
이것은 의미가 없습니다. 보안을 위해 OO 캡슐화 http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) – gertas