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 方法來實現私有屬性的獲取和更改,起到了對私有屬性的保護做用。this

在上面的代碼中,咱們還寫了一個 toString() 方法,可是咱們並無在 Person 類中定義該方法,這是由於當咱們定義 Person 類的時候,系統會默認繼承 Object 類,且 Object 類中有 toString() 方法,而且輸出爲一個 類名@地址,這個字符串沒有什麼實際意義。所以一般咱們要使用一個類的 toString 方法時,就應當重寫該方法,重寫該方法後,返回的字符串沒有嚴格的格式要求,未來能夠根據需求而定,可是原則上該字符串應當包含當前對象的屬性信息,只有當咱們自定義的類須要重寫該方法時,JAVA API 提供的類一般都已經重寫了該方法。spa

 下面咱們將 toString() 方法進行重寫:code

 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 方法,意思是比較兩個對象,以下:blog

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。

相關文章
相關標籤/搜索