ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ ๋์ ๊ฐ์ฒด์ ๋ด์ฉ์ ์์๋ด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. ์ด๋ฌํ ๋ชฉ์ ์ ์ํด ํด๋์ค ๊ณ์ธต ๊ตฌ์กฐ์์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ Java ํด๋์ค์ธ Java Object ํด๋์ค๋ ํน๋ณํ toString() ๋ฉ์๋๋ฅผ ์ ์ํฉ๋๋ค. ๊ฐ์ฒด๋ฅผ ์ค๋ช
ํ๋ ๋ฌธ์์ด์ ๋ฐํํฉ๋๋ค. Java์์ toString() ๋ฉ์๋ ์ ๊ธฐ๋ณธ ๊ตฌํ์ ๋ค์๊ณผ ๊ฐ์ ์ถ๋ ฅ์ ์์ฑํฉ๋๋ค.
package.class@hashCode
์ฆ , java.lang.Object ์ ์ ์๋ toString ์ ๊ฐ์ฒด์ ํด์ ์ฝ๋๋ฅผ 16์ง์ ํ์์ผ๋ก ์ ๊ณตํฉ๋๋ค. ์ด๋ ๊ทธ๋ค์ง ๋ช
ํํ์ง๋ ์ ์ฉํ์ง๋ ์์ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. ๋ฐ๋ผ์ ์ ํด๋์ค๋ฅผ ์์ฑํ ๋ ๋ฐํ ๋ฌธ์์ด์ ํด๋์ค ์ด๋ฆ, ๋ชจ๋ ๋ณ์์ ์ด๋ฆ ๋ฐ ๊ฐ์ด ํฌํจ๋๋๋ก toString()์ ์ฌ์ ์ํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์
๋๋ค. toString ๋ฉ์๋๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ฌ์ ์ํ๋ฉด Java ํ๋ก๊ทธ๋จ์ ๋ก๊น
ํ๊ณ ๋๋ฒ๊น
ํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ผ๋ฉฐ ๊ท์คํ๊ณ ์ค์ํ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค. ๊ฐ๋จํ ๋งํด์, toString() ๋ฉ์๋๋ Java์์ ์ซ์ ๊ฐ์ฒด์ ๊ฐ์ ๋ํ๋ด๋ ๋ฌธ์์ด ๊ฐ์ฒด๋ฅผ ์ป๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์ฆ, ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํฉ๋๋ค. ๋ฉ์๋๊ฐ ๋จ์ ๋ฐ์ดํฐ ์ ํ์ ์ธ์๋ก ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋จ์ ๋ฐ์ดํฐ ์ ํ์ ๊ฐ์ ๋ํ๋ด๋ ๋ฌธ์์ด ๊ฐ์ฒด๊ฐ ๋ฐํ๋ฉ๋๋ค.
Java toString() ๋ฉ์๋์ ์
์ผ๋ฐ์ ์ผ๋ก ํด๋์ค๋ฅผ ์์ฑํ๋ ๊ฒฝ์ฐ Java์ toString() ๋ฉ์๋ ์ธ์๋ ํด๋น ๊ฐ์ฒด๋ก ์์ ํ๋ ๋ฐฉ๋ฒ์ ์๊ธฐ ์ํด equals , hashCode ๋ฐ CompareTo ๋ฉ์๋๋ฅผ ์ฌ์ ์ํด์ผ ํฉ๋๋ค . ํ์ง๋ง ์ง๊ธ์ toString ์ ์ง์คํ๊ฒ ์ต๋๋ค . ๋๋ถ๋ถ์ ๊ฒฝ์ฐ ๊ฐ์ฒด์ ๋ํ ํ ์คํธ ์ ๋ณด๋ฅผ ์ ์ถํ๋ ๊ฒ์ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. ์ฌ์ฉ์๊ฐ ๋ก๊น ํ ๋ ์ ํํ ๋ฌด์์ ๋ณด๊ณ ์ถ์ดํ๋์ง ์ดํดํ๋ฉด ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด ํ๋ฉด์ ํ ์ ์ ์ค๋ช ํ๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค. ๋ ๊ฐ์ ์ขํ(coordinateX,coordinateY)๊ฐ ์์ต๋๋ค. ๋น์ฐํ ํด๋์ค ์ด๋ฆ์ฒ๋ผ ์ฌ์ฉ์๊ฐ "์ด๊ฒ ๋ฌด์จ ๊ฐ์ฒด์ง?"๋ผ๊ณ ๋ฌผ์ผ๋ฉด ํ๋ฉด์ ํ์๋์ด์ผ ํฉ๋๋ค. ์ด๊ฒ์ด ์ฌ์ ์๋ toString() ๋ฉ์๋ ์์ ์์ฐํ ๋ด์ฉ์ ๋๋ค . ์ด์ ํด๋์ค ์์ฒด, ํด๋น ํ๋(์ขํ), ์์ฑ์, ์ขํ๋ฅผ ์ป๊ธฐ ์ํ getter๋ฅผ ๋ง๋ญ๋๋ค(๋ฌผ๋ก ํด๋์ค๋ฅผ ๋ง๋ค์ง ์์ ์๋ ์์ง๋ง OOP์ ์ฌ๋ฐ๋ฅธ ์ฌ์ฉ ๊ด์ ์์ ๋ณด๋ฉด getter๊ฐ ์ข์ ์ต๊ด์ ๋๋ค). ์ค๋ช ์ ์ํด ๋ค๋ฅธ ๋ ํฌ์ธํธ์ ํฉ์ธ ์ ํฌ์ธํธ๋ฅผ ์์ฑํ๊ณ equals ๋ฐ hashCode ๋ฉ์๋๋ฅผ ์ฌ์ ์ํ๋ ๋ฉ์๋๋ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.public class Point implements Comparable<Point> {
private final int coordinateX;
private final int coordinateY;
public Point(final int coordinateX, final int coordinateY) {
this.coordinateX = coordinateX;
this.coordinateY = coordinateY;
}
public int getX() {
return coordinateX;
}
public int getY() {
return coordinateY;
}
public Point sum(final Point that) {
return new Point(coordinateX + that.coordinateX, coordinateY + that.coordinateY);
}
// here we override toString with coordinates and class name
@Override
public String toString() {
return "Point{"
+ "X: " + getX()
+ ", Y: " + getY()
+ '}';
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o == null || o.getClass() != this.getClass()) {
return false;
}
@Override
public int hashCode() {
return coordinateX + coordinateY;
}
์ด์ ํ
์คํธ ํด๋์ค์ ๊ธฐ๋ณธ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค. ๊ฑฐ๊ธฐ์๋ ๋ ๊ฐ์ ์ ์ด ์๊ณ ์ธ ๋ฒ์งธ ์ ์ ๋ ์ ์ ํฉ์์ ์ป์ต๋๋ค. ์ฌ๊ธฐ์ toString ๋ฉ์๋๋ฅผ ์ ์ฉํ์ฌ ์ถ๋ ฅํด๋ณด๊ฒ ์ต๋๋ค.
//toString() method demo
public class Test {
public static void main(String[] args) {
Point myPoint1 = new Point(5,7);
Point myPoint2 = new Point(-2, 8);
Point mySum = myPoint1.sum(myPoint2);
//call toString explicitly
System.out.println(mySum.toString());
}
}
์ด ํ๋ก๊ทธ๋จ ์์
์ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
ํฌ์ธํธ{X: 3, Y: 15}
์ค์ ๋ก toString ๋ฉ์๋๋ฅผ ๋ช
์์ ์ผ๋ก ํธ์ถํ ํ์๋ ์์ผ๋ฉฐ ๋๋ถ๋ถ ํธ์ถํ์ง ์์ต๋๋ค. toString() ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ค๋ฉด ์ํ๋ ๊ฐ์ฒด๋ฅผ System.out.println ์ ์ ๋ฌํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค . ๋ฐ๋ผ์ ํ
์คํธ ํด๋์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
//toString method demo
public class Test {
public static void main(String[] args) {
Point myPoint1 = new Point(5,7);
Point myPoint2 = new Point(-2, 8);
Point mySum = myPoint1.sum(myPoint2);
//call toString implicitly
System.out.println(mySum);
}
}
ํ๋ก๊ทธ๋จ์ ์ถ๋ ฅ์ ์ด์ ๊ณผ ๋์ผํฉ๋๋ค. toString ๋ฉ์๋๋ฅผ ์ฌ์ ์ํ์ง ์๊ณ ๊ฐ์ฒด๋ฅผ ์ธ์ํ๊ณ ์ถ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น์? ์ด ๊ฒฝ์ฐ Object ํด๋์ค ์ toString ๋ฉ์๋๊ฐ ํธ์ถ๋ฉ๋๋ค. ์ด ์์์๋ Point ํด๋์ค ์์ toString ๋ฉ์๋๋ฅผ ์ ๊ฑฐ ํ๊ณ ๋ชจ๋ ๊ฒ์ ํ
์คํธ ํด๋์ค์ ๊ทธ๋๋ก ๋๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ฒ ๋ฉ๋๋ค.
ํฌ์ธํธ@12
์ฌ๊ธฐ์ 16์ง์๋ก ๋ ๊ฐ์ฒด์ ํด์ ์ฝ๋๊ฐ ์์ต๋๋ค.
GO TO FULL VERSION