Core Java(十二) Object类的重要方法

释放双眼,带上耳机,听听看~!

Object是所有类的最终祖先。,可以使用Object类引用任何类型的对象。在Java中,只有基本类型不是对象。

Equals方法

Object类中的equals方法用于检测一个对象是否等于另外一个对象。在Object类中,这个方法将判断两个对象是否具有相同的引用。

但是,我们一般意义上的相等不仅仅是具有相同的引用,如果另个对象的状态相等,就认为另个对象是相等的。所以,子类要重写超级类中的equals方法。

Java中,要求equals方法具有下面的特性:

  1. 自反性,x.equals(x)返回ture。
  2. 对称性,x.equals(y),和y.equals(x)返回相同的值。
  3. 传递性,对于任何引用x,y,z,如果x.equals(y)返回true,y.equals(z)返回true,那么x.equals(z)也要返回true。
  4. 一致性,如果x和y所引用的对象没有变化,反复调用x.equals(y)应该返回同样的结果。
  5. 对于任意非空引用x,x.euqals(null)应该返回false。

如下例,实现了俩个equals方法:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
1package com.xujin;
2
3import java.util.Arrays;
4
5public class Test {
6   public static void main(String[] args) {
7       Employee employee = new Employee("Bod", 5000);
8       Employee staff = employee;
9       Manager manager = new Manager("Bod", 8000, 6000);
10      System.out.println(employee.equals(new Employee("Bod", 5000)));//true
11      System.out.println(employee.equals(staff));//true
12      System.out.println(employee.equals(null));//false
13      System.out.println(manager.equals(new Manager("Bod", 8000, 6000)));//true
14      System.out.println(employee.equals(manager));//false
15     
16      System.out.println(employee instanceof Employee);//true
17      System.out.println(employee instanceof Manager);//false
18      System.out.println(manager instanceof Employee);//true
19     
20     
21      //考察两个数组是否相同
22      System.out.println(Arrays.equals(new int[]{1,2,3}, new int[]{1,2,3}));//true
23  }  
24}
25
26class Employee{
27  public Employee(String name, double salary){
28      this.name = name;      
29      this.salary = salary;      
30  }  
31 
32  public boolean equals(Object otherObject){
33      //如果两个对象具有相同的引用,返回true
34      if(this == otherObject)
35          return true;
36      //如果otherObjext是null,返回false
37      if(otherObject == null)
38          return false;
39      //如果两个对象所属的类不相同,返回false
40      if(this.getClass() != otherObject.getClass())
41          return false;
42     
43      //此时,两个对象既不是null,他们所属的类也相同,都为Employee
44      Employee other = (Employee)otherObject;
45      return name.equals(other.name) && salary == other.salary;
46     
47  }
48 
49  //定义变量
50  private double salary;
51  private String name;
52}
53
54class Manager extends Employee{
55  public Manager(String name, double salary, double bonus){
56      super(name, salary);
57      this.bonus = bonus;
58  }
59 
60  public boolean equals(Object otherObject){
61      if(!super.equals(otherObject))
62          return false;
63      Manager other = (Manager)otherObject;
64      return bonus == other.bonus;
65  }
66 
67  private double bonus;
68}
69

HashCode方法

散列吗,是由对象导出的一个整型值。此方法是有Object定义的,返回该对象的存储地址。所以,如果有必要,子类要对HashCode方法进行重写。

注意:如果重写了equals方法,则必须重写HashCode方法,因为这两个方法必须一致:
如果equals方法返回true,则HashCode方法必须返回相同值。

下例中,String类已经重写了HashCode方法,使用对象的值导出散列码,而StringBuilder方法没有重写HashCode方法,返回的依然是存储单元的地址。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1package com.xujin;
2
3public class Main{
4   public static void main(String...args){
5       String s1 = "Hello,world!";
6       String s2 = new String("Hello,world!");
7       System.out.println(s1.hashCode());//2007142665
8       System.out.println(s2.hashCode());//2007142665
9      
10      StringBuilder sb1 = new StringBuilder("Hello,world!");
11      StringBuilder sb2 = new StringBuilder(s1);
12      System.out.println(sb1.hashCode());//1291383602
13      System.out.println(sb2.hashCode());//1814462232
14  }
15}
16

toString方法

在Object类中,它用于返回对象所属类名和散列码,例如


1
2
3
4
5
6
7
8
1package com.xujin;
2
3public class Main{
4   public static void main(String...args){
5       System.out.println(System.out);//java.io.PrintStream@6102d81c
6   }
7}
8

1
2
1 输出的是一个java.io.PrintStream@6102d81c,这是因为System.out所属的PrintStream类并没有覆盖Object的toString方法。
2

而在大多数情况下,它用于返回表示对象值的字符串。

绝大多数的toString方法都遵循这个格式: 类名[域值,域值,域值……]

如下例:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1package com.xujin;
2
3public class Main{
4   public static void main(String...args){
5       Employee staff = new Employee("Bob",8000);
6       System.out.println(staff.toString());
7   }
8}
9
10class Employee{
11  public Employee(String name, double salary){
12      this.name = name;      
13      this.salary = salary;      
14  }  
15 
16  public String toString(){
17      return getClass().getName() + "[name = " + name + ", salary = " + salary + "]";
18  }
19 
20  //定义变量
21  private double salary;
22  private String name;
23}
24

1
2
1 输出将是:com.xujin.Employee[name = Bob, salary = 8000.0]
2

其实,只要对象与一个字符串通过操作符“+”连接起来,Java编译就会自动的调用toString方法,以便获得这个对象的字符串描述。

给TA打赏
共{{data.count}}人
人已打赏
安全技术

详解Node.js API系列 Crypto加密模块

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索