Java 从入门到进阶之路(十八)

在之前的文章我们介绍了一下 Java 中的正则表达式,本章我们来看一下 Java 中的 Object。

在日常生活中,任何事物我们都可以看做是一个对象,在编程中是同样的道理,在 Java 编程中其实更突出,因为 Java 就是一门面向对象的编程语言。

我们先来看下面的代码:

 1 public class Main {
 2     public static void main(String[] args) {
 3         Person person = new Person();
 4 //        person.age = 1000; // 编译错误
 5 //        System.out.println(person.age); // 编译错误
 6         person.setAge(1000);
 7         System.out.println(person.getAge()); // 0
 8 
 9         person.setAge(10);
10         System.out.println(person.getAge()); // 10
11 
12         System.out.println(person.toString()); // Person@1b6d3586
13     }
14 }
15 
16 class Person {
17     private int age;
18 
19     public int getAge() {
20         return age;
21     }
22 
23     public void setAge(int age) {
24         if (age < 0 || age > 100) {
25             return;
26         }
27         this.age = age;
28     }
29 }
30     }
31 
32     public void setAge(int age) {
33         if (age < 0 || age > 100 ) {
34             return;
35         }
36         this.age = age;
37     }
38 }

在上面的代码中,我们定义了 get 和 set 方法来实现私有属性的获取和更改,起到了对私有属性的保护作用。

在上面的代码中,我们还写了一个 toString() 方法,但是我们并没有在 Person 类中定义该方法,这是因为当我们定义 Person 类的时候,系统会默认继承 Object 类,且 Object 类中有 toString() 方法,并且输出为一个 类名@地址,这个字符串没有什么实际意义。因此通常我们要使用一个类的 toString 方法时,就应当重写该方法,重写该方法后,返回的字符串没有严格的格式要求,将来可以根据需求而定,但是原则上该字符串应当包含当前对象的属性信息,只有当我们自定义的类需要重写该方法时,JAVA API 提供的类通常都已经重写了该方法。

 下面我们将 toString() 方法进行重写:

 1 public class Main {
 2     public static void main(String[] args) {
 3         Point point = new Point(1, 2);
 4         String string = point.toString();
 5         System.out.println(string); // (1,2)
 6     }
 7 }
 8 
 9 class Point {
10     private int x;
11     private int y;
12 
13     public int getX() {
14         return x;
15     }
16 
17     public void setX(int x) {
18         this.x = x;
19     }
20 
21     public int getY() {
22         return y;
23     }
24 
25     public void setY(int y) {
26         this.y = y;
27     }
28 
29     public Point() {
30     }
31 
32     public Point(int x, int y) {
33         this.x = x;
34         this.y = y;
35     }
36 
37     public String toString() {
38         return "(" + x + "," + y + ')';
39     }
40 }

在上面的代码中,我们定义了一个 Point 类,相当于二维坐标系上的一个点,我们通过重写 toString 方法实现了一个坐标点的位置。

在 Object 类中还有定义好的 equals 方法,意思是比较两个对象,如下:

public class Main {
    public static void main(String[] args) {
        Point point1 = new Point(1, 2);
        Point point2 = new Point(1, 2);
        System.out.println(point1 == point2); // false
        System.out.println(point1.equals(point2)); // false
    }
}

在上面的代码中,point1 == point2 其实比较的是两个类的引用地址,所以为 false,我们看一下 equals 方法的源码:

1   public boolean equals(Object obj) {
2         return (this == obj);
3     }

在上面的代码中,Object 类的 equals 方法其实也是相当于 == 的方法来进行比较,所以当我们使用 equals 方法时同样需要进行重写,他的作用是比较两个对象(当前对象与给定对象)内容是否一样,如下:

 1 public class Main {
 2     public static void main(String[] args) {
 3         Point point1 = new Point(1, 2);
 4         Point point2 = new Point(1, 2);
 5         System.out.println(point1 == point2); // false
 6         System.out.println(point1.equals(point2)); // true
 7     }
 8 }
 9 
10 class Point {
11     private int x;
12     private int y;
13 
14     public int getX() {
15         return x;
16     }
17 
18     public void setX(int x) {
19         this.x = x;
20     }
21 
22     public int getY() {
23         return y;
24     }
25 
26     public void setY(int y) {
27         this.y = y;
28     }
29 
30     public Point() {
31     }
32 
33     public Point(int x, int y) {
34         this.x = x;
35         this.y = y;
36     }
37 
38     public String toString() {
39         return "(" + x + "," + y + ')';
40     }
41 
42     public boolean equals(Object object) {
43         if (object == null) {
44             return false;
45         }
46         if (object == this) {
47             return true;
48         }
49         if (object instanceof Point) {
50             Point point = (Point) object; // 强转为point类型
51             return this.x == point.x && this.y == point.y;
52         }
53         return false;
54     }
55 }

当我们重写 equals 方法后,就可以获取我们想要的结果了,即上面代码第 6 行结果输出为 true。同样的,只有自己定义的类需要重写,JAVA API 提供的类基本都重写了 equals。

posted @ 2019-12-27 10:55  丰寸  阅读(433)  评论(0编辑  收藏  举报