一.toString()方法
Object类是类层次结构的根(父)类,每个类都使用Object类作为超类,所有对象都实现这个类的方法,其中toString()方法,默认输出的是类名和内存地址值。
在Object类中里面定义toString方法的时候 返回的对象是哈希code码,这个哈希code码不能简单明了的表示出对象的属性,所以要重写toString方法。
(1)当我们打印一个对象的引用时,默认的打印的是一个对象的toString方法;
(2)当我们打印的类没有重写Object类 的toString()方法时,默认调用 Object类的toString()方法;
(3)当我们打印对象所在的类重写了toString方法时,此时调用的就是我们重写的toString()方法;
(4)我们重写toString方法的格式为:将对象的属性信息打印出来,也可以通过eclipse中的source导入;
(5)像String,包装类 ,File类,Date类等,都已经重写了toString()方法。
二.equals()方法
1.自反性:对于任何非空引用x,x.equals(x)都应该返回true;
2.对称性:对于任何引用x,y如果x.equals(y)返回true,那么y.equals(x)也应该返回true;
3传递性:如果x.equals(y),y.equals(z)都返回true,那么,x.equals(z)返回true;
4一致性:如果x,y引用的对象没有发生变化,那么无论调用多少次x.equals(y)都返回相同的结果。
Object类中的equals方法用于检测两个对象是否相等 ,而相等判断的准则是两者是否引用同一个对象。
下面给出编写一个完美的equals方法的原则:
1:将equals的Object类显示参数命名为otherObject。
2:检测otherObject是否与this引用自同一个对象。
3:检测otherObject是否为null,是的话返回false。
4:如果在子类中重新定义equals方法,就要在其中包含调super.equals(otherObject)。
5:检测this与otherObject是否属于同一个类,这里需要注意的情况就是当比较的两个 对象不属于同一个类,相等的概念是由父类决定的,比如认为只要name和age相同,则Person和Employee就可以认为相等了,则可以使用instanceof进行检测,同时在父类中将该方法定义为final。
6:将otherObject转换为相应的类类型变量。
7:现在开始进行私有域的比较,对于基本类型使用==进行比较,对于对象域则使用equals进行比较,所有域都相同则返回true。
8:使用@Override声明重写的equals方法有助于错误检查
三.代码示例
package one;
class Person{
String name;
int age;
Person(){//无参构造方法
name="Marry";
age=21;
}
Person(String n,int a){//两个参数的构造方法
name=n;
age=a;
}
void info(){//获取信息的方法
System.out.println("name:"+name);
System.out.println("age="+age);
}
public String toString(){//重写Object类的toString方法
System.out.println("name:"+name+" "+"age="+age);
return (name+" "+age);
}
public boolean equals(Object obj){
if(obj instanceof Person) {
Person person = (Person) obj;
if(this.name.equals(person.name) && this.age == person.age) {
System.out.println(this.age + "的" + this.name + "和" +person.age + "的" + person.name + "是同一个人");
}else {
System.out.println(this.age + "的" + this.name + "和" +person.age + "的" + person.name + "不是同一个人");
}
}else {
System.out.println("对象(" + obj + ")不是Person类或者其子类的实例");
}
System.out.println("---------------------------");
return false;
}
}
class Teacher extends Person{
String title;
Teacher(String n,int a){
this(n,a,"讲师");//调用自身三个参数的构造方法
}
Teacher(String n,int a,String t){
super(n,a);//调用父类两个参数的构造方法
title=t;
}
void info(){//重写父类获取信息的方法
System.out.println("name:"+name);
System.out.println("age="+age);
System.out.println("tilte:"+title);
}
public String toString() {
System.out.println("name:" + name + " " + "age:" + age + " "+ "title:" + title);
return (name + " "+ age+ " "+ title);
}
public boolean equals(Object obj) {
if(obj instanceof Teacher) {
Teacher teacher = (Teacher) obj;
if(this.name.equals(teacher.name) && this.age == teacher.age && this.title.equals(teacher.title)) {
System.out.println(this.age + "的" + this.title + this.name + "和" +teacher.age + "的" + teacher.title + teacher.name + "是同一个人");
}else {
System.out.println(this.age + "的" + this.title + this.name + "和" +teacher.age + "的" + teacher.title + teacher.name + "不是同一个人");
}
}else {
System.out.println("对象(" + obj + ")不是Teacher类或者其子类的实例");
}
return false;
}
}
public class Tset {
public static void main(String[] args){
Person pe1=new Person();
Person pe2=new Person("coco",18);
Teacher te1=new Teacher("vita",22);
Teacher te2=new Teacher("selina",30,"教授");
pe1.info();
pe1.toString();
pe2.info();
pe2.toString();
pe1.equals(pe2);
te1.info();
te1.toString();
te2.info();
te2.toString();
te1.equals(te2);
}
}