Object类中常见方法——toString(),equals()使用
package com.tedu.obj;
public class Point {
private int x,y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString() {
return ("("+ x + " ," + y + ")");
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
代码测试
package com.tedu.obj;
import java.util.Arrays;
public class TestPoint {
public static void main(String[] args) {
Point p = new Point(100,220);
String str = p.toString();
System.out.println(str);
Point[] arr = {new Point(100,200),
new Point(100,210),
new Point(100,220),
new Point(100,230),
new Point(100,240)
};
String line = Arrays.toString(arr);
System.out.println(line);
Point p0 = new Point(100,220);
System.out.println(p == p0);
System.out.println(p.equals(p0));
}
}