在之前的文章我們介紹了一下 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。