⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uppas40.htm

📁 对于学习很有帮助
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Delphi 4增訂的Object Pascal</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<font FACE="Arial" SIZE="6">

<p ALIGN="CENTER">Delphi 4</font><font SIZE="6">增訂的</font><font FACE="Arial"
SIZE="6">Object Pascal</font><font FACE="Times New Roman" SIZE="1"></p>

<p ALIGN="JUSTIFY"></font><font SIZE="1"> </font><font face="宋体" size="4">本附錄在於說明Delphi 
4新增訂的Object Pascal程式語言。總結來說,Delphi 4的Object Pascal增訂了以下幾大部分:</font></p>

<blockquote>
  <p ALIGN="JUSTIFY"><font face="宋体" size="4">新增加數種內定資料型態。</font></p>
  <p ALIGN="JUSTIFY"><font face="宋体" size="4">Dynamic Arrays</font></p>
  <p ALIGN="JUSTIFY"><font face="宋体" size="4">Method and Routine Overloading</font></p>
  <p ALIGN="JUSTIFY"><font face="宋体" size="4">Default Parameters.</font></p>
  <p ALIGN="JUSTIFY"><font face="宋体" size="4">Implementating interfaces by delegation,可用以下的寫法:</font></p>
</blockquote>

<p><font face="宋体" size="4"> </font></p>

<p><font face="宋体" size="4">property MyInterface: IMyInterface read FMyInterface <br>
implements IMyInterface;</font></p>

<p><font face="宋体" size="4">新增的內定資料型態</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">整數方面:</font></p>

<blockquote>
  <p ALIGN="JUSTIFY"><font face="宋体" size="4">新增加<i>Int64</i>這種長度六十四位元,範圍從-2^63 
  to 2^63 - 1。</font></p>
  <p ALIGN="JUSTIFY"><font face="宋体" size="4">32-bit unsigned integer新增加<i>Longword</i>型態,範圍從0..4294967295。</font></p>
  <i><p ALIGN="JUSTIFY"><font face="宋体" size="4">Cardinal</i>的範圍調整成與上述<i>Longword</i>型態相同。</font></p>
</blockquote>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">浮點數方面:</font></p>

<blockquote>
  <i><p ALIGN="JUSTIFY"><font face="宋体" size="4">Real</i>型態基於效率考量,由過去佔用48-bit調整成64-bits,</font></p>
  <p ALIGN="JUSTIFY"><font face="宋体" size="4">新增加<i>Real48</i>型態,精確度與過去的<i>Real</i>相同,仍是48-bit。</font></p>
</blockquote>

<p><font face="宋体" size="4"> </font></p>

<p><font face="宋体" size="4">Dynamic Array</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">Delphi 4問世之前,Delphi程式設計師若需要動態長度的陣列,在不得已的情況下,往往採用以下這種「表面上陣列的語法,但實際自己配置管理記憶體」的方式來處理動態陣列:</font></p>

<p><font face="宋体" size="4">#0001 procedure TForm1.Button1Click(Sender: TObject);</font></p>

<p><font face="宋体" size="4">#0002 type</font></p>

<p><font face="宋体" size="4">#0003 TIntegerArray = array[0..0] of integer; </font></p>

<p><font face="宋体" size="4">#0004 PIntegerArray = ^TIntegerArray;</font></p>

<p><font face="宋体" size="4">#0005 var</font></p>

<p><font face="宋体" size="4">#0006 pArrayOfInteger: PIntegerArray; </font></p>

<p><font face="宋体" size="4">#0007 j: integer;</font></p>

<p><font face="宋体" size="4">#0008 begin</font></p>

<p><font face="宋体" size="4">#0009 GetMem(pArrayOfInteger, 10 * SizeOf(Integer)); </font></p>

<p><font face="宋体" size="4">#0010 for j := 0 to 9 do</font></p>

<p><font face="宋体" size="4">#0011 pArrayOfInteger^[j] := j; </font></p>

<p><font face="宋体" size="4">#0012 FreeMem(pArrayOfInteger, 10 * SizeOf(Integer));</font></p>

<p><font face="宋体" size="4">#0013 end;</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">如果應用Delphi 4新增加的Dynamic 
Array,則可改用以下的方式:</font></p>

<p><font face="宋体" size="4">var MyFlexibleArray: array of Real;</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">定義之後,以 <i>SetLength</i> 
函數改變陣列實際配置的記憶體大小,例如:</font></p>

<p><font face="宋体" size="4">SetLength(MyFlexibleArray, 20); // 0..19</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">方便很多,是嗎?若想知道Dynamic 
Array實際長度,請分別以<i>High</i>與<i>Low</i>函數判斷,傳回「-1」時,表示是一個該陣列的長度為零,例如:</font></p>

<p><font face="宋体" size="4"> </font></p>

<p><font face="宋体" size="4">#0001 procedure TForm1.Button2Click(Sender: TObject);</font></p>

<p><font face="宋体" size="4">#0002 var</font></p>

<p><font face="宋体" size="4">#0003 A: array of Integer;</font></p>

<p><font face="宋体" size="4">#0004 begin</font></p>

<p><font face="宋体" size="4">#0005 ShowMessage(IntToStr(High(A))); // <b>-1</b></font></p>

<p><font face="宋体" size="4">#0006 SetLength(A, 3);</font></p>

<p><font face="宋体" size="4">#0007 ShowMessage(IntToStr(High(A))); // 3</font></p>

<p><font face="宋体" size="4">#0008 A := nil;</font></p>

<p><font face="宋体" size="4">#0009 ShowMessage(IntToStr(High(A))); // -1</font></p>

<p><font face="宋体" size="4">#0010 end;</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">上述的0008這列,指定<i>nil</i>值將會釋放陣列所配置到的記憶體。</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">值得注意的是,Compiler對於Dynamic 
Array並不會自動進行所謂的「Copy-on-Write」。請看以下的程式例:</font></p>

<p><font face="宋体" size="4">#0001 procedure TForm1.Button1Click(Sender: TObject);</font></p>

<p><font face="宋体" size="4">#0002 var</font></p>

<p><font face="宋体" size="4">#0003 A, B: array of Integer;</font></p>

<p><font face="宋体" size="4">#0004 begin</font></p>

<p><font face="宋体" size="4">#0005 SetLength(A, 1);</font></p>

<p><font face="宋体" size="4">#0006 A[0] := 1;</font></p>

<p><font face="宋体" size="4">#0007 B := A;</font></p>

<p><font face="宋体" size="4">#0008 B[0] := 2;</font></p>

<p><font face="宋体" size="4">#0009 ShowMessage(IntToStr(A[0])); // ==&gt; 2</font></p>

<p><font face="宋体" size="4">#0010 end;</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">0008這列改的雖是<i>B[0]</i>,但Compiler顯然還沒有聰明到在可能修改陣列內容時,將陣列內容複製一份出來(Copy 
on (possible) Write),於是,0009這列程式顯示的結果仍是2。</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">於是,若要進行兩陣列各索引項目一對一的內容複製,只好自行寫迴圈一一複製,或者,採用較為簡便的寫法 
── 呼叫Copy函數:</font></p>

<p><font face="宋体" size="4">#0001 procedure TForm1.Button4Click(Sender: TObject);</font></p>

<p><font face="宋体" size="4">#0002 var</font></p>

<p><font face="宋体" size="4">#0003 A, B: array of Integer;</font></p>

<p><font face="宋体" size="4">#0004 begin</font></p>

<p><font face="宋体" size="4">#0005 SetLength(A, 1);</font></p>

<p><font face="宋体" size="4">#0006 A[0] := 1;</font></p>

<p><font face="宋体" size="4">#0007 B := Copy(A, 0, 1);</font></p>

<p><font face="宋体" size="4">#0008 B[0] := 2;</font></p>

<p><font face="宋体" size="4">#0009 ShowMessage(IntToStr(A[0])); // ==&gt; 1</font></p>

<p><font face="宋体" size="4">#0010 ShowMessage(IntToStr(B[0])); // ==&gt; 2</font></p>

<p><font face="宋体" size="4">#0011 end;</font></p>

<p ALIGN="JUSTIFY"><font face="宋体" size="4">最後,請不要將Dynamic Array與第二章提到的Open 
Array(開放陣列)搞混了。函式參數的開放陣列,語法雖與Dynamic Array差不多,但開放陣列指的是:不限定傳入函式的陣列長度,Dynamic 
Array則是指:可變長度的陣列,兩者畢竟有所不同。以下是一則我準備的程式範例,不難看出兩者之間的差異:</font><b></p>

<p><font face="宋体" size="4">#0001 type</font></p>

<p><font face="宋体" size="4">#0002 TDynamicInteger = array of integer;</font></b></p>

<p><font face="宋体" size="4">#0003 </font></p>

<p><font face="宋体" size="4"> </font><b></p>

<p><font face="宋体" size="4">#0004 procedure Clear_OpenArray(var A: array of Integer);</font></b></p>

<p><font face="宋体" size="4">#0005 var</font></p>

<p><font face="宋体" size="4">#0006 I: Integer;</font></p>

<p><font face="宋体" size="4">#0007 begin</font></p>

<p><font face="宋体" size="4">#0008 for I := 0 to High(A) do A[I] := 0;</font></p>

<p><font face="宋体" size="4">#0009 // SetLength(A, 20); // 不可以這麼寫</font></p>

<p><font face="宋体" size="4">#0010 end;</font></p>

<p><font face="宋体" size="4">#0011 </font></p>

<p><font face="宋体" size="4">#0012 // 注意, 以下兩種寫法又有不同喔</font></p>

<p><font face="宋体" size="4">#0013 // I: procedure Clear_DynamicArray(A: 
TDynamicInteger);</font></p>

<p><font face="宋体" size="4">#0014 // II: procedure Clear_DynamicArray(var A: 
TDynamicInteger);</font></p>

<p><font face="宋体" size="4"> </font><b></p>

<p><font face="宋体" size="4">#0015 procedure Clear_DynamicArray(var A: 
TDynamicInteger);</font></b></p>

<p><font face="宋体" size="4">#0016 var</font></p>

<p><font face="宋体" size="4">#0017 I: Integer;</font></p>

<p><font face="宋体" size="4">#0018 begin</font></p>

<p><font face="宋体" size="4">#0019 for I := 0 to High(A) do A[I] := 0;</font></p>

<p><font face="宋体" size="4">#0020 SetLength(A, 20); // 如果是Dynamic Array, 
可以這麼寫</font></p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -