객체의 참조 변수를 호출할 때, 자동 호출되는 특이한 메서드
자식이 오버라이드 해서 사용 가능
1. 동일성 검증 코드
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);
System.out.println(c2);
// 내용 비교 : 주소는 다르지만 값은 같다는 것을 증명 - equals()
System.out.println(c1 == c2);
System.out.println(c1.toString().equals(c2.toString()));
}
}
결과

Share article