编写equals 的建议:
1.测试this与otherobject是否是同一对象
if (this==otherobject) return true;
2.测试otherobject是否为null.如果是,就返回false.
if (otherobject==null) return false;
3.测试this和otherobject是否同属于一个类
if (getclas()!=otherobject.getclass()) return false;
4. 把otherobject的类型转换为你的类所属的类型
ClassName other=(ClassName)otherobject
5.比较所有字段.使用==比较基本类型字段,使用equals比较对象字段
return fields1==other.fields && field2.equals(other.field2)&&............
tostring 方法
object的另一个重要方法就是tostring ,它返回一个代表该对象值字符串.
大多数(不是全部)tostring 方法都遵循如下格式:类名,然后在方括号中列举个字段的值.
无论何时对象与字符串相连接,那么就可以使用"+"操作符,这时JAVA编译器会自动调用tostring 方法获得对象的字符串表示.