作者dasea (植栽雞肉飯)
看板ncyu_phyedu
標題[討論] 訊息傳遞
時間Sat Jan 30 14:56:05 2010
訊息傳遞(Message Passing)和函數呼叫(Function Call)的不同點
Procedure Language以函數作為寫作和執行的主體。函數由一群程式碼所組成,函數的開
頭地址在Compile time或Link time就已經決定好了,而在Runtime呼叫函數時給予適當的
參數。函數和記憶體之間沒有關聯性,而且函數A可以被任何其他函數呼叫,程式語言並沒
有特別的規範。
Object-Oriented Language以Class作為寫作的主體,執行時主要由Object來紀錄程式狀態
。由於物件導向程式語言將Object Variable和Object Method一起定義在Class內,再透過
Encapsulation的機制規範存取Object Member的範圍,因此Object的Variable和Method就
組成了一個完整的個體。
雖然Object Method寫起來就像Function,但執行Method內程式碼的機制和Function不同:
Object Method定義物件接受到訊息時的反應, 也就是說執行Method時有一個隱形的參數
, 意即這個物件(this)
Function的實際地址在Compile或Link time就已經決定好了, 但對OO來說, Object必須在
Runtime接受到訊息後, 才能決定實際要執行的Method。
Message Passing的語法
由於Java只能透過reference來存取物件,因此Message Passing的語法是
reference.method
/**
* 假設People有variable age,其值限定於0到130之間
*/
public class People {
private int age;
public People(int d) {
this.age = d;
}
public int getAge() {
return age;
}
public void setAge(int d) {
if (d >= 0 && d <= 130) { // 檢查年齡是否合法
age = d;
}
}
public void increaseAge() {
if (age < 130) { // 檢查年齡是否合法
this.age++;
}
}
public static void main(String[] argv) {
People e1, e2; // e1,e2 are references to Object People, not the
Object themselves
e1 = new People(3);
e2 = new People(5);
e1.setAge(30);
e2.setAge(50);
e1.increaseAge();
e2.increaseAge();
}
}
this這個keyword表示接收到此訊息的物件。由於設計時是定義Class, 而執行時則是
Object接受訊息, Class只有一個, 但Object可以有很多個, 因此必須使用this表達現在
接收到此訊息的物件。
由於Object Method具有隱形的物件參數, 因此Class Method不能去存取Object Member
public class ErrorCall {
int data; // object variable
public void objectMethod() {
data = 10;
}
public static void classMethod() {
objectMethod(); // Compile Error
data = 10; // Compile Error
}
}
Message Passing在執行期間才會決定實際Method的機制, 我們將在Inheritance(繼承)裡
敘述。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.58.22.74