Super Coding Addict
Ch10. 자바 기본 클래스 (3) - Object 클래스 본문
- 예제
package ch10.object;
class Student{
int studentNum;
String studentName;
public Student(int studentNum, String studentName) {
this.studentNum = studentNum;
this.studentName = studentName;
}
@Override
public boolean equals(Object obj) {
if( obj instanceof Student) {
Student std = (Student)obj; //다운캐스팅
return (this.studentNum == std.studentNum);
}
return false;
}
@Override
public int hashCode() {
return studentNum;
}
}
public class EqualsTest {
public static void main(String[] args) {
Student Lee = new Student(100, "이순신");
Student Lee2 = Lee; //주소 같음
Student Shin = new Student(100, "이순신");
System.out.println(Lee == Shin); //false (물리적 주소 다름)
System.out.println(Lee.equals(Shin));
System.out.println(Lee.hashCode());
System.out.println(Shin.hashCode());
Integer i1 = 100;
Integer i2 = 100;
System.out.println(i1.equals(i2));
System.out.println(i1.hashCode());
System.out.println(i2.hashCode());
System.out.println(System.identityHashCode(i1));
System.out.println(System.identityHashCode(i2));
}
}
--> 아래 코드와 같이 equals()메서드에 이어 hashcode()메서드를 재정의
@Override
public boolean equals(Object obj) {
if( obj instanceof Student) {
Student std = (Student)obj; //다운캐스팅
return (this.studentNum == std.studentNum);
}
return false;
}
@Override
public int hashCode() {
return studentNum;
}
=> hashCode()메서드를 호출하면 studentNum을 반환한다
--> 메모리상의 실제 해쉬코드를 찍어보고 싶다면, System클래스의 static메서드인 System.identityHashCode()를 호출하면 된다.
* clone() 메서드
- 객체의 복사본을 만듦
- 기본틀(prototype)으로부터 같은 속성 값을 가진 객체의 복사본을 생성할 수 있음
- instance 상태를 그대로 복제해 오는 것!
=> 정보은닉 위배의 가능성 => 복제할 객체는 cloneable 인터페이스를 명시해야 함
- 예제 : Book 클론하기
package ch10.object;
class Book implements Cloneable{
String title;
String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
@Override
public String toString() {
return author + ", " + title;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
}
// --> 직접 부르는 게 아니라,
// 이 객체가 힙 메모리에서 해제될 때 GC에서 호출되는 메서드
// 리소스 해제, 안닫혔있는 소켓 닫는 등의 일을 수행
}
public class ToStringTest {
public static void main(String[] args) throws CloneNotSupportedException {
Book book = new Book("토지", "박경리");
System.out.println(book);
Book book2 = (Book)book.clone();
System.out.println(book2);
}
}
--> 아래와 같이 clone()메서드를 재정의할 수 있으나, 원래 clone()메서드 자체에 인스턴스 복사가 있으므로 따로 재정의하지 않고 super.clone()으로 원래 clone()메서드를 그대로 유지
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
--> 중요한 것은, 여기서 clone()메서드만 오버라이딩한다고 끝나는 게 아니라, 복제할 객체에 대해 cloneable 인터페이스를 명시해주어야 한다는 것이다!
--> 이렇게 명시를 하지 않으면, CloneNotSupportedException을 뱉어내며 복제가 되지 않는다
'JAVA 문법' 카테고리의 다른 글
Ch10. 자바 기본 클래스 (5) - 코딩해보기 (0) | 2021.03.02 |
---|---|
Ch10. 자바 기본 클래스 (4) - Class 클래스 (0) | 2021.03.01 |
Ch10. 자바 기본 클래스 (2) - Object 클래스 (0) | 2021.03.01 |
Ch10. 자바 기본 클래스 (1) - Object 클래스 (0) | 2021.02.28 |
Ch12. 내부 클래스(2) (0) | 2021.02.27 |