๊ฐ์ฒด๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ๊ธฐ
class Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point("+x+","+y+")";
}
}
public class ObjectEx {
static void print(Object obj) {
System.out.println(obj.getClass());
System.out.println(obj.hashCode());
System.out.println(obj.toString());
System.out.println(obj);
}
public static void main(String[] args) {
Point p = new Point(2, 3);
print(p);
}
}
๊ฒฐ๊ณผ๊ฐ ์คํธ๋ง์ ์ญ์ Point(2,3)์ผ๋ก ์ถ๋ ฅ๋จ.
equal ๋น๊ต
class Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point("+x+","+y+")";
}
}
public class ObjectEx {
static void print(Object obj) {
Point p = new Point(2, 3);
System.out.println(obj.getClass());
System.out.println(obj.hashCode());
System.out.println(obj.toString());
System.out.println(obj);
System.out.println("p: "+p.hashCode());
System.out.println(obj == p);
System.out.println(obj.equals(p));
}
public static void main(String[] args) {
Point p = new Point(2, 3);
print(p);
}
}
๊ฒฐ๊ณผ
๊ธฐ๋ณธ p๋ฅผ ๋ณํํ๋ค๋ฉด
class Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point("+x+","+y+")";
}
@Override
public boolean equals(Object obj) {
Point p2 = new Point(2, 3);
if(p2.x == x && p2.y == y)
return true;
else
return false;
}
}
public class ObjectEx {
static void print(Object obj) {
Point p = new Point(2, 3);
System.out.println(obj.getClass());
System.out.println(obj.hashCode());
System.out.println(obj.toString());
System.out.println(obj);
System.out.println("p: "+p.hashCode());
System.out.println(obj == p);
System.out.println(obj.equals(p));
}
public static void main(String[] args) {
Point p = new Point(2, 3);
print(p);
}
}
728x90
๋ฐ์ํ
0