Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

pungjoo

작성중 - toString() 본문

JAVA

작성중 - toString()

pungjoo.kim 2010. 7. 9. 16:46
0. 들어가면서

통상 객체의 필드의 내용을 확인 하는 용도로 System.out.println( someClass.toString() ); 을 많이 이용합니다.
그러나 toString을 Override를 하지 않으면 'className@hashcode' 값이됩니다.


1. 준비 운동

package info.yeonwoo.edu;

public class User {
	
	private String	name;
	private int	age;
	private String	address;
	
	public String getName() {
		return name;
	}
	
	public void setName( String name ) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge( int age ) {
		this.age = age;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress( String address ) {
		this.address = address;
	}
	
	public static void main( String... args ) {

		User user = new User();
		user.setName( "pungjoo.kim" );
		user.setAddress( "우리별." );
		user.setAge( 35 );
		
		System.out.println( user );
		
	}
}

결과는 다음과 같습니다.

info.yeonwoo.edu.User@19f953d


2. toString method를 Override.

일반적으로 위와 같은 경우 toString을 Override를 해 원하는 형태로 만들어 줍니다.  

	@Override
	public String toString() {

		StringWriter sw = new StringWriter();
		PrintWriter out = new PrintWriter( sw );
		
		out.println( super.toString() + " [" );
		
		out.print( "\t" );
		out.println( "name : " + getName() );
		out.print( "\t" );
		out.println( "address : " + getAddress() );
		out.print( "\t" );
		out.println( "age : " + getAge() );
		out.println( "]");
		out.println();
		
		return sw.toString();
		
	}

결과는 다음과 같습니다.
info.yeonwoo.edu.User@1eed786 [
	name : pungjoo.kim
	address : 우리별.
	age : 35
]


3. 매번 모든 class의 toString을 Override 하고 싶지 않고..

package info.yeonwoo.edu;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;

public abstract class ToString {
	
	public String toString() {

		StringWriter sw = new StringWriter();
		PrintWriter out = new PrintWriter( sw, true );
		
		out.println( super.toString() + " [" );
		Method[] methods = this.getClass().getMethods();
		
		for ( Method method : methods ) {
			
			if ( ( method.getName().startsWith( "get" ) || method.getName().startsWith( "is" ) ) && !method.getName().equals( "getClass" )
					&& method.getParameterTypes().length == 0 ) {
				
				if ( method.getName().startsWith( "get" ) ) {
					out.print( "\t" + method.getName().substring( 3 ) + " : " );
				}
				else {
					out.print( "\t" + method.getName().substring( 2 ) + " : " );
				}
				
				try {
					out.println( method.invoke( this ) );
				}
				catch ( Exception e ) {
				}
				
			}
			
		}
		
		out.println( "]" );
		
		return sw.toString();
		
	}
	
}
추상 class를 하나 만들고.

본래 source를 다음과 같이 변경합니다.
package info.yeonwoo.edu;

public class User extends ToString {
	
	private String	name;
	private int	age;
	private String	address;
	
	public String getName() {
		return name;
	}
	
	public void setName( String name ) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge( int age ) {
		this.age = age;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress( String address ) {
		this.address = address;
	}
	
	public static void main( String... args ) {

		User user = new User();
		user.setName( "pungjoo.kim" );
		user.setAddress( "우리별." );
		user.setAge( 35 );
		
		System.out.println( user.toString() );
		
	}
}
결과는 Method를 Override한 것과 비슷합니다.

어쩌구 저쩌구 해서 저쩌구 하지 뭐
Comments