Object类是一个特殊的类,是所有类的父类,如果一个类没有用extends明确指出继承于某个类,那么它默认继承Object类。这里主要总结Object类中的三个常用方法:toString()、equals()、hashCode()。
1.取得对象信息的方法:toString()
该方法在打印对象时被调用,将对象信息变为字符串返回,默认输出对象地址。
class Student
{
String name = "Mary";int age = 21;
}public class Text{
public static void main(String[] args){
Student s = new Student();System.out.println("姓名:"+s.name+",年龄:"+s.age);//输出对象属性System.out.println(s);//直接输出对象信息System.out.println(s.toString());//调用父类方法输出对象信息}
}
输出结果:
姓名:Mary,年龄:21
ClassNotes.Student@15db9742
ClassNotes.Student@15db9742
上述结果看出编译器默认调用toString()方法输出对象,但输出的是对象的地址,我们并不能看懂它的意思。那么就要通过重写Object类的toString()方法来输出对象属性信息。
class Student
{
String name = "Mary";int age = 21;public String toString(){
return "姓名:"+name+",年龄:"+age;}
}
输出结果:姓名:Mary,年龄:21。这样对象信息就更加清晰了。
2.对象相等判断方法:equals()
该方法用于比较对象是否相等,而且此方法必须被重写。
class Student
{
String name;int age;public Student(String name,int age){
this.name=name;this.age=age;}
}public class Text{
public static void main(String[] args){
Student s1 = new Student("Mary",21);Student s2 = new Student("Mary",21);System.out.println(s1.equals(s2));//输出一个boolean值System.out.println(s1.equals(s2)?"s1和s2是同一个人":"s1和s2不是同一个人");//?:条件运算符}
}
输出结果:s1和s2不是同一个人。
很明显输出的结果是错误的,因为equals()方法比较的是两个对象的地址,所以必须重写方法才能到达目的。
//重写父类(Object类)中的equals方法
public boolean equals(Object o)
{
boolean temp = true;Student s1 = this;if(o instanceof Object){
Student s2 = (Student)o;if(!(s1.name.equals(s2.name)&&s1.age==s2.age)){
temp = false;}}else{
temp = false;}return temp;//返回一个布尔值
}
3.对象签名:hashCode()
该方法用来返回其所在对象的物理地址(哈希码值),常会和equals方法同时重写,确保相等的两个对象拥有相等的.hashCode。
class Student
{
String name;int age;//重写父类(Object类)中的equals方法public boolean equals(){
boolean temp;Student s1 = new Student();s1.name="张三";s1.age=12;Student s2 = new Student();s2.name="张三";s2.age=12;System.out.println("s1的哈希码:"+s1.hashCode());System.out.println("s2的哈希码:"+s2.hashCode());if((s1.name.equals(s2.name))&&(s1.age==s2.age)){
temp = true;}else{
temp = false;}return temp;}//重写hashCode()方法public int hashCode(){
return age*(name.hashCode());}
}public class Text{
public static void main(String[] args){
Student s3 = new Student();System.out.println(s3.equals()?"s1和s2是同一人":"s1和s2不是同一人");}
}
输出结果:
s1的哈希码:9298668
s2的哈希码:9298668
s1和s2是同一人
4.深克隆与浅克隆
浅克隆
- 是指拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象。
深克隆
- 不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;class Wife implements Serializable{
private String name;private Date birthday;public Wife(){
name = "芙蓉姐姐";birthday = new Date();}public Date getBirthday(){
return birthday;}public String getName() {
return name;}public void setName(String name) {
this.name = name;}
}
class Husband implements Cloneable,Serializable{
private Wife wife;private Date birthday;public Husband(){
wife = new Wife();birthday = new Date();}public Wife getWife(){
return wife;}public Date getBirthday(){
return birthday;}/*** 浅克隆一个对象*/public Object clone() {
Husband husband = null;try{
husband = (Husband)super.clone();}catch(CloneNotSupportedException e){
e.printStackTrace();}finally{
return husband;}}/*** 利用串行化深克隆一个对象,把对象以及它的引用读到流里,在写入其他的对象* @return* @throws IOException* @throws ClassNotFoundException*/public Object deepClone() throws IOException,ClassNotFoundException {
//将对象写到流里ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectOutputStream oos = new ObjectOutputStream(bos);oos.writeObject(this);//从流里读回来ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());ObjectInputStream ois = new ObjectInputStream(bis);return ois.readObject();}
}
public class Test {
public static void main(String[] args){
try{
Husband husband = new Husband();System.out.println("husband birthday "+husband.getBirthday().getTime());System.out.println("wife birthday "+husband.getWife().getBirthday().getTime());System.out.println();Husband husband1 = (Husband)husband.clone();System.out.println("husband1 birthday "+husband1.getBirthday().getTime());System.out.println("wife birthday "+husband1.getWife().getBirthday().getTime());System.out.println();System.out.println("是否是同一个husband "+(husband == husband1));System.out.println("是否是同一个wife "+ (husband.getWife() == husband1.getWife()));System.out.println();Husband husband2 = (Husband)husband.deepClone();System.out.println("husband2 birthday "+husband2.getBirthday().getTime());System.out.println("wife birthday "+husband2.getWife().getBirthday().getTime());System.out.println();System.out.println("是否是同一个husband "+(husband == husband2));System.out.println("是否是同一个wife "+ (husband.getWife() == husband2.getWife()));}catch(Exception e){
e.printStackTrace();}}
}
运行结果:
husband birthday 1414247244668
wife birthday 1414247244668
husband1 birthday 1414247244668
wife birthday 1414247244668
是否是同一个husband false
是否是同一个wife true
husband2 birthday 1414247244668
wife birthday 1414247244668
是否是同一个husband false
是否是同一个wife false