Contents
1. 통일성 검증객체의 참조 변수를 호출할 때, 자동 호출되는 특이한 메서드이다.
자식이 오버라이드해서, 사용할 수 있다.
1. 통일성 검증
toSring을 재정의 하여 클래스 안에 있는 값을 비교하여 ture / false를 출력한다.
package ex17;
class Card {
private int no;
private String name;
private String content;
public Card(int no, String name, String content) {
this.no = no;
this.name = name;
this.content = content;
}
public int getNo() {
return no;
}
public String getName() {
return name;
}
public String getContent() {
return content;
}
@Override
public String toString() {
return "Card{" +
"no=" + no +
", name='" + name + '\'' +
", content='" + content + '\'' +
'}';
}
}
public class ToS01 {
public static void main(String[] args) {
Card c1 = new Card(1, "드래곤볼", "손오공");
Card c2 = new Card(1, "드래곤볼", "손오공");
System.out.println(c1.hashCode());
System.out.println(c2.hashCode());
System.out.println(c1);
System.out.println(c2);
//toString 이용하여 동일성 검사
System.out.println(c1.toString().equals(c2.toString()));
}
}

Share article