線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:906
推到 Plurk!
推到 Facebook!

請教程式區塊的問題

尚未結案
sumeihua
一般會員


發表:9
回覆:13
積分:4
註冊:2004-04-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-28 23:13:37 IP:63.84.xxx.xxx 未訂閱
請教各位先進:    程式區塊(Block)中, 如果型態, 常數, 變數的宣告位置不同, 各程序對于其的使用也有相同的使用權限, 如下為一個Unit(即Unit1)的基本內容, 我加了一些變數的宣告(紅色部分), 勞煩各位先進指點以下孌數宣告部分相對應的解釋是否正确, 及其未注明處的使用權限和區塊區別.
Unit Unit1;    interface    Uses
...[略]    type
  TForm1 = Class(TForm)
  ...[略]
  Private
    A : Interger; //此處的變數宣告只能用于這個Unit(即Unit1), 其它的Unit不能使用此變數
  {Private declarations}
  Public
    B : Integer; //此處的變數宣告除了可以在這個Unit(即Unit1)中使用, 也可被其它的Unit使用, 即可在整個專案(Project)中使用
  {Public declarations}
  end;    Var
  Form1 : TForm1;
  C : Integer; //不知此處的孌數宣告适用于何處?     Implementation    {$R *.DFM}    Procedure TForm1.FormCreate(Sender: TObject);
Var
  D : Integer; //此處的變數宣告只能用于FormCreate事件中, 其它事件的程序不能使用
begin
  D := 100;
end;    Procedure TForm1.Button1Click(Sender : TObject);
  begin
    //ShowMessage(IntToStr(A));
    //ShowMessage(IntToStr(B));
    //ShowMessage(IntToStr(C));
    //以上三個變數在ShowMessage時, 是否都可顯示出其對應的值? 有何區別
  end;    end.
謝謝各位先進! ====================================== 誠心向學! 肯請指教! ======================================
------
======================================
誠心向學! 肯請指教!
======================================
qoo1234
版主


發表:256
回覆:1167
積分:659
註冊:2003-02-24

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-29 10:11:07 IP:218.163.xxx.xxx 未訂閱
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;    type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Memo3: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    A : Integer;
    A1: String;
    { Private declarations }
  public
     B : Integer;
     B1: String; 
    { Public declarations }
  end;    var
  Form1: TForm1;
  C : Integer;
  C1: String;
implementation
  uses Unit2;
{$R *.dfm}    procedure TForm1.Button1Click(Sender: TObject);
begin
 Memo1.Lines.Add('A (整數):' IntToStr(A));
 Memo1.Lines.Add('B (整數):' IntToStr(B));
 Memo1.Lines.Add('C (整數):' IntToStr(C));
 Memo1.Lines.Add('A1(字串):' A1);
 Memo1.Lines.Add('B1(字串):' B1);
 Memo1.Lines.Add('C1(字串):' C1);
end;    procedure TForm1.FormCreate(Sender: TObject);
begin
  A := 20; A1:='a';
  B := 40; B1:='b';
  C := 60; C1:='c';
end;    procedure TForm1.Button2Click(Sender: TObject);
begin
 Form2.Show;
end;    end.
======================================================================
unit Unit2;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Unit1;    type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form2: TForm2;    implementation    {$R *.dfm}    procedure TForm2.Button1Click(Sender: TObject);
begin
   //Form1.Memo2.Lines.Add('A(整數):' IntToStr(A));
   //Form1.Memo2.Lines.Add('B(整數):' IntToStr(B));
   Form1.Memo2.Lines.Add('C(整數):' IntToStr(C));       //Form1.Memo2.Lines.Add('A1(字串):' A1);
   //Form1.Memo2.Lines.Add('B1(字串):' B1); 
   Form1.Memo2.Lines.Add('C1(字串):' C1);
end;    procedure TForm2.Button2Click(Sender: TObject);
begin
 with Form1 do 
 begin
   //Memo3.Lines.Add('A (整數):' IntToStr(A));
   Memo3.Lines.Add('B (整數):' IntToStr(B)); //等同於 Form1.B
   Memo3.Lines.Add('C (整數):' IntToStr(C));
   //Memo3.Lines.Add('A1(字串):' A1);
   Memo3.Lines.Add('B1(字串):' B1);          //等同於 Form1.B1
   Memo3.Lines.Add('C1(字串):' C1);
 end;
end;    end.
 
Form1結果: A (整數):20 B (整數):40 C (整數):60 A1(字串):a B1(字串):b C1(字串):c Form2結果: C(整數):60 C1(字串):c Form2結果(使用With Form1 do): B (整數):40 C (整數):60 B1(字串):b C1(字串):c 發表人 - qoo1234 於 2004/04/29 10:16:13
sumeihua
一般會員


發表:9
回覆:13
積分:4
註冊:2004-04-24

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-29 10:26:06 IP:63.84.xxx.xxx 未訂閱
謝謝qoo1234先進的指教﹒    在Form2中的兩段程式碼(Button1Click和Button2Click)中﹐紅色注明的部分是否為注解﹖    可否勞煩解釋一下其中的原因﹖    謝謝您﹗    ====================================== 誠心向學! 肯請指教! ======================================
------
======================================
誠心向學! 肯請指教!
======================================
qoo1234
版主


發表:256
回覆:1167
積分:659
註冊:2003-02-24

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-04-29 10:31:32 IP:218.163.xxx.xxx 未訂閱
引言: 謝謝qoo1234先進的指教﹒ 在Form2中的兩段程式碼(Button1Click和Button2Click)中﹐紅色注明的部分是否為注解﹖ 可否勞煩解釋一下其中的原因﹖ 謝謝您﹗ ====================================== 誠心向學! 肯請指教! ======================================
紅色注明的部分 表示錯誤寫法
sumeihua
一般會員


發表:9
回覆:13
積分:4
註冊:2004-04-24

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-04-29 11:07:37 IP:63.84.xxx.xxx 未訂閱
謝謝qoo1234先進﹒    從先進的範例來看﹐C和C1的宣告是屬性整個專案的﹐即在專案各個單元皆可使用﹒而B和B1是宣告在Public中的﹐那在Unit2的Button1Click中為何不能使用﹖而在Unit2的Button2Click中倒可以使用呢﹖關鍵似乎是出現在With Form1 do這句上﹐可否勞煩先進略作解釋﹒ 謝謝﹗ ====================================== 誠心向學! 肯請指教! ======================================
------
======================================
誠心向學! 肯請指教!
======================================
sumeihua
一般會員


發表:9
回覆:13
積分:4
註冊:2004-04-24

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-04-29 18:07:13 IP:63.84.xxx.xxx 未訂閱
經過測試﹐了解其意義了﹕    在Unit2的Button2.OnClick事件程式碼中的With Form1 do[red]即是在下列使用到的Form1的外部變數前加上Form1.﹐使其成為﹕[red]Memo3.Lines.Add('B (整數):' IntToStr(Form1.B)); 從而使Unit2可使用宣告在Form1中的外部變數B﹒ 謝謝qoo1234先進的指點﹒ ====================================== 誠心向學! 肯請指教! ====================================== 發表人 - sumeihua 於 2004/04/29 18:18:27
------
======================================
誠心向學! 肯請指教!
======================================
系統時間:2024-05-15 23:24:13
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!