首先定义Point测试类:
package day02;
/**
* 使用该类来测试Object相关方法的重写。
* 该类的每一个实例表示直角坐标系上的一个点。
* @author kaixu
*
*/
public class Point {
private int x;
private int 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 Point(){
}
public Point(int x,int y){
this.x = x;
this.y = y;
}
/**
* 通常我们要使用一个类的toString方法时,就应当重写该方法。
* Object自身的toString方法返回的是:类名@地址
* 这个字符串本身没有什么实际意义。
* 重写该方法后,返回的字符串没有严格的格式要求,将来可以根据需求而定。
* 但是原则上该字符串中应当包含当前对象的属性信息。
* 只有我们自定义的类需要重写该方法,JAVA API提供的类都已经重写了该方法。
*/
public String toString(){
return "("+x+","+y+")";
}
/**
* 通常,当我们需要使用一个类的equals方法时就应当重写它。
* equals方法的作用是比较当前对象与给定对象内容是否一样(长得像不像)。
* Object提供的equals方法内部就是用"=="实现的,所以没有意义。
* equals比较时不一定要求所有属性值都要相同,这要结合实际需求而定。
* 只有自己定义的类需要重写,JAVA API提供的类基本上都重写了equals
*/
public boolean equals(Object obj){
if(obj == null){
return false;
}
if(obj == this){
return true;
}
if(obj instanceof Point){
Point p = (Point)obj;
return this.x==p.x&&this.y==p.y;
}
return false;
}
}
在此基础上利用TestPoint类来测试相关的重写方法是否有效:
package day02;
/**
* 测试Point类重写Object相关方法
* @author kaixu
*
*/
public class TestPoint {
public static void main(String[] args) {
Point p1 = new Point(1,2);
//String str = p1.toString();
//System.out.println(str);
/*
* System.out.println(Object obj)
* 输出的是给定对象toString返回的字符串
*/
System.out.println(p1);
Point p2 = new Point(1,2);
System.out.println(p1==p2);
System.out.println(p1.equals(p2));
}
}