객체를 문자열로 표현한 값
public class SmartPhone /* extends Object */ {
private String company;
private String os;
public SmartPhone(String company, String os) {
this.company = company;
this.os = os;
}
//보통 Object클래스의 toString()메소드를 재정의를 할 경우에는 클래스의 필드정보 나타내는 목적으로 주로 한다
@Override
public String toString() {
// TODO Auto-generated method stub
return company + ", " + os;
}
}
public class SmartPhoneExample {
public static void main(String[] args) {
SmartPhone myPhone = new SmartPhone("구글", "안드로이드");
// 재정의 안된 경우 출력 : sec03.exam03_toString.SmartPhone@15db9742 객체를 문자열로 표현한 형태
// 재정의 된 경우 출력: 구글, 안드로이드
System.out.println(myPhone.toString());
}
}