- 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() 四個方法
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()); } } /* 《程式語言教學誌》的範例程式 http://pydoing.blogspot.com/ 檔名:JungleDemo8.java 功能:示範物件導向的基本觀念 作者:張凱慶 時間:西元 2010 年 10 月 */
編譯後執行,結果如下
中英文術語對照 | |
---|---|
類別 | class |
物件 | object |
方法 | method |
資源回收者 | garbage collector |
改寫 | override |
參考資料
http://download.oracle.com/javase/tutorial/java/IandI/objectclass.html
http://download.oracle.com/javase/7/docs/api/java/lang/Object.html
http://java.sun.com/docs/books/jls/third_edition/html/classes.html
http://download.oracle.com/javase/tutorial/java/IandI/objectclass.html
http://download.oracle.com/javase/7/docs/api/java/lang/Object.html
http://java.sun.com/docs/books/jls/third_edition/html/classes.html
沒有留言:
張貼留言