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不同:
由於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(繼承)裡敘述。