在類別宣告中加一個type所指的意思為何?? |
答題得分者是:timhuang
|
chimera
初階會員 發表:62 回覆:78 積分:28 註冊:2003-03-22 發送簡訊給我 |
|
timhuang
尊榮會員 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
Hi, 引述一段 online help 上的說明:
Type identity is almost straightforward. When one type identifier is declared using another type identifier, without qualification, they denote the same type. Thus, given the declarations type T1 = Integer; T2 = T1; T3 = Integer; T4 = T2; T1, T2, T3, T4, and Integer all denote the same type. To create distinct types, repeat the word type in the declaration. For example, type TMyInteger = type Integer; creates a new type called TMyInteger which is not identical to Integer.簡單的說, 使用了 type 後該 type 就與原來的定義型別是不相同的(當然還是相容的, 只是在建構時, 會當作不一樣的. 舉個最簡單的例子. type myInt = Integer; myInt2 = type Integer; function myfunc(var d: integer): integer; begin ShowMessage(IntToStr(d)); Result := d; end; function myfunc2(d: integer): integer; begin ShowMessage(IntToStr(d)); Result := d; end;上面的 myfunc 是不能傳入 myInt2 的, 但是可以傳入 myInt 的(即是 Integer), 但 myInt2 就不會視為 Integer, 但是第二個 myfunc2 則是傳入 myInt 或 myInt2 都可以, 因為是利用 copy constructor 來進行. 也就是說: myInt i; myInt2 i2; i:=10; //OK i2:=10; //也 ok. 設定值與讀出值是與 integer 相容的. myfunc(i); //OK myfunc(i2); // compiler 時就 error, 因為要傳入的是位址(call by reference) myfunc2(i); //OK myfunc2(i2); //也 ok. 原因是因為會先進行 d:=i2 後再開始 myfunc2 的程式 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |