From Wikipedia, the free encyclopedia
物件生命週期(英文:object lifetime)係物件導向編程上嘅一個概念,指一件物件由「被創造」至「被剷走」之間嘅嗰段時間。當一個程式創建一件物件嗰陣,部電腦內部會搵個記憶體位置嚟裝住件物件嘅數據-「數據」包括件物件啲特性同方法呀噉,而呢個時間點就係件物件嘅生命週期嘅起始點;喺個程式行嘅期間,個程式叫親部電腦用件物件,部機都會由件物件屬嘅記憶體位置攞數據用;而件物件相應嗰啲數據通常會喺三種情況下被剷走[1][2]:
class Complex {
... // 其他拉雜碼...
// 定義個建構子...
public Complex()
{
real = 0;
img = 0;
}
... // 其他拉雜碼...
~Complex() // 定義 Complex 呢個類別嘅解構子,個解構子喺被執行嗰陣會令部電腦彈「Destructor was called」噉嘅字眼出嚟(方便編程員睇同用)。
{
Console.WriteLine("Destructor was called");
}
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.