Java 快速導覽 - 物件導向概念 物件之母 Object

Java 裡所有的類別 (class) 都繼承自 Object 這個類別,因此所有物件 (object) 均可使用 Object 的方法 (method) 。 Object 方法如下

  • protected Object clone() throws CloneNotSupportedException
  • public boolean equals(Object obj)
  • protected void finalize() throws Throwable
  • public final Class getClass()
  • public int hashCode()
  • public String toString()
  • public final void notify()
  • public final void notifyAll()
  • public final void wait()
  • public final void wait(long timeout)
  • public final void wait(long timeout, int nanos)


功能如下表
clone()建立並回傳物件的副本
equals()比較是否為相同物件
finalize()呼叫資源回收者 (garbage collector) 檢查此物件是否應該被回收
getClass()取得類別名稱
hashCode()取得物件的雜湊碼
toString()取得物件的字串表示形式
notify()有關同步功能....
notifyAll()有關同步功能....
wait()有關同步功能....


final 的方法不能再改寫 (override) ,非 final 的方法可以改寫成符合自己需要的。


以下程式示範使用 equals() 、 getClass() 、 hashCode() 、 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class Animal {
    private int age;
    private int weight;
     
    public Animal(int a, int w) {
        setAge(a);
        setWeight(w);
        System.out.println("使用兩個參數的建構子,Animal物件已建立....");
    }
     
    public Animal(int w) {
        this(3, w);
    }
 
    public Animal() {
        this(3, 15);
    }
     
     
    public int getAge() {
        return age;
    }
 
    public void setAge(int n) {
        if (n < 0) {
            age = 1;
        }
        else {
            age = n;
        }
    }
     
    public int getWeight() {
        return weight;
    }
     
    public void setWeight(int n) {
        if (n < 0) {
            weight = 1;
        }
        else {
            weight = n;
        }
    }
     
    public void speak() {
        System.out.println("哈囉,我已經" + getAge() + "歲,有" + getWeight() + "公斤重");
    }
}
 
class Elephant extends Animal {
    private String name;
     
    public Elephant(String n, int a, int w) {
        super(a, w);
        setName(n);
        System.out.println("使用三個參數的建構子,Elephant物件已建立....");
    }
     
    public Elephant(int a, int w) {
        this("小象", a, w);
    }
     
    public Elephant() {
        this("小象", 3, 150);
    }
     
    public String getName() {
        return name;
    }
     
    public void setName(String n) {
        if (n == null || n.equals("")) {
            name = "無名氏";
        }
        else {
            name = n;
        }
    }
     
    public void speak() {
        super.speak();
        System.out.println("我的名字是" + getName());
    }
}
 
class JungleDemo8 {
    public static void main(String[] args) {
        Elephant puppy1 = new Elephant("大象", 6, 70);
        Elephant puppy2 = new Elephant(10, 142);
        Animal puppy3 = new Animal();
         
        if (puppy1.equals(puppy2)) {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }
         
        if (puppy3.equals(puppy2)) {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }
 
        System.out.println(puppy2.getClass());
        System.out.println(puppy3.getClass());
        System.out.println(puppy2.hashCode());
        System.out.println(puppy3.hashCode());
        System.out.println(puppy2.toString());
        System.out.println(puppy3.toString());
    }
}
 
/* 《程式語言教學誌》的範例程式
    檔名:JungleDemo8.java
    功能:示範物件導向的基本觀念
    作者:張凱慶
    時間:西元 2010 年 10 月 */


編譯後執行,結果如下



中英文術語對照
類別class
物件object
方法method
資源回收者garbage collector
改寫override






沒有留言: