全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1011
推到 Plurk!
推到 Facebook!

Interface實作討論

尚未結案
dhting
一般會員


發表:6
回覆:20
積分:10
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-30 20:09:50 IP:61.225.xxx.xxx 未訂閱
大家好,      最近看了介紹Interface的文章後, 自己試著實作時, 有些問題想提出大家討論一下, 也許是自己對Interface的瞭解不深用錯了, 或是誤解了Interface的用法, 希望能藉此糾正自己的觀念.   程式的目的是希望透過Interface來達到傳送文字訊息到主Form, 雖然有人會說直接use主form不就得了, 但是我只是想透這interface來做看看, 畢竟在某些情況下可能可以成為solution.(例如:package加MDI, 子form想與主form溝通時)      首先create一個interface如下, 很簡單地只是一個procedure的宣告:    unit IStatusTextUnit;    interface    type   IStatusText = interface     procedure IStatusPut(AText: String);   end;    implementation    end.      接著create一個Factory unit(作用我個人認為是要將interface物件化), 由於IStatusPut的實作在主form上, 所以這裡要use mainForm    unit StatusFactoryUnit;    interface    uses IStatusTextUnit, mainForm;    type   Tfrm1Factory = class     class function Create: IStatusText;   end;    implementation    class function Tfrm1Factory.Create: IStatusText; begin   result := TForm1.Create(Form1); //這裡Create的參數對嗎? end; end. 最後, 在主form的程式碼中如下: unit mainForm; interface uses IStatusTextUnit, Windows.............; type TForm1 = class(TForm, IStatusText) .................... private { Private declarations } procedure IStatusPut(AText: String); public { Public declarations } end; .................. procedure TForm1.IStatusPut(AText: String); begin Form1.StatusBar1.Panels[0].Text := AText //這裡要加Form1才能顯示出文字 // StatusBar1.Panels[0].Text := AText; end; end. 我的問題是紅字部份, interface的實作程式碼是在Form1之中, 可是如果不加上Form1則無法順利顯示, 是否表示interface實際上產生了另一個Form1呢? 希望能引起共嗚.
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-10-01 08:51:35 IP:210.3.xxx.xxx 未訂閱
What is your purpose in creating the factory unit? I think the following should work fine for you:    
unit IStatusTextUnit;    interface    type
  IStatusText = interface
    procedure IStatusPut(AText: String);
  end;    implementation    end.    unit mainForm;    interface    uses
  IStatusTextUnit,
  Windows.............;    type
  TForm1 = class(TForm, IStatusText)
    ....................
  private
    { Private declarations }
    procedure IStatusPut(AText: String);
  public
    { Public declarations }
  end;    ..................    procedure TForm1.IStatusPut(AText: String);
begin
  StatusBar1.Panels[0].Text := AText;
end;    end.
dhting
一般會員


發表:6
回覆:20
積分:10
註冊:2002-04-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-10-01 10:17:42 IP:210.241.xxx.xxx 未訂閱
引言: 大家好, 最近看了介紹Interface的文章後, 自己試著實作時, 有些問題想提出大家討論一下, 也許是自己對Interface的瞭解不深用錯了, 或是誤解了Interface的用法, 希望能藉此糾正自己的觀念. 程式的目的是希望透過Interface來達到傳送文字訊息到主Form, 雖然有人會說直接use主form不就得了, 但是我只是想透這interface來做看看, 畢竟在某些情況下可能可以成為solution.(例如:package加MDI, 子form想與主form溝通時) 首先create一個interface如下, 很簡單地只是一個procedure的宣告: unit IStatusTextUnit; interface type IStatusText = interface procedure IStatusPut(AText: String); end; implementation end. 接著create一個Factory unit(作用我個人認為是要將interface物件化), 由於IStatusPut的實作在主form上, 所以這裡要use mainForm unit StatusFactoryUnit; interface uses IStatusTextUnit, mainForm; type Tfrm1Factory = class class function Create: IStatusText; end; implementation class function Tfrm1Factory.Create: IStatusText; begin result := TForm1.Create(Form1); //這裡Create的參數對嗎? end; end. 最後, 在主form的程式碼中如下: unit mainForm; interface uses IStatusTextUnit, Windows.............; type TForm1 = class(TForm, IStatusText) .................... private { Private declarations } procedure IStatusPut(AText: String); public { Public declarations } end; .................. procedure TForm1.IStatusPut(AText: String); begin Form1.StatusBar1.Panels[0].Text := AText //這裡要加Form1才能顯示出文字 // StatusBar1.Panels[0].Text := AText; end; end. 我的問題是紅字部份, interface的實作程式碼是在Form1之中, 可是如果不加上Form1則無法順利顯示, 是否表示interface實際上產生了另一個Form1呢? 希望能引起共嗚.
對不起大家... 我少放了一段程式..就是子Form要傳文字回主Form unit frm1unit; interface uses IStatusTextUnit, Windows, ............. type TForm2 = class(TForm) Button1: TButton; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure Button1Click(Sender: TObject); private { Private declarations } FStatus: IStatusText; public { Public declarations } end; var Form2: TForm2; implementation uses StatusFactoryUnit; {$R *.dfm} procedure TForm2.Button1Click(Sender: TObject); begin FStatus := Tfrm1Factory.Create; FStatus.IStatusPut('test'); end; end. 這也是為什麼我要create一個Factory unit的原因
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-10-02 09:22:39 IP:147.8.xxx.xxx 未訂閱
I think in your case there is really no point in using interface... using simple inheritance should be enough. For interface, maybe this example can give you some insight:
IMyInterface = interface
    procedure MyCall;
end;    TObjectA = class(TAutoObject, IMyInterface)
    procedure MyCall;
end;    TObjectB = class(TAutoObject, IMyInterface)
    procedure MyCall;
end;    { ... }
var
    A: TObjectA;
    B: TObjectB;
{ ... }
(A as IMyInterface).MyCall;
(B as IMyInterface).MyCall;
dhting
一般會員


發表:6
回覆:20
積分:10
註冊:2002-04-15

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-10-02 22:48:01 IP:61.216.xxx.xxx 未訂閱
引言: I think in your case there is really no point in using interface... using simple inheritance should be enough. For interface, maybe this example can give you some insight:
IMyInterface = interface
    procedure MyCall;
end;    TObjectA = class(TAutoObject, IMyInterface)
    procedure MyCall;
end;    TObjectB = class(TAutoObject, IMyInterface)
    procedure MyCall;
end;    { ... }
var
    A: TObjectA;
    B: TObjectB;
{ ... }
(A as IMyInterface).MyCall;
(B as IMyInterface).MyCall;
不好意思, 才疏學淺的我不太懂您的意思.. Interface不是只能宣告而沒有實作嗎? 那麼object A與B的MyCall實作程式碼該放在那裡呢? 可否請willima兄多作說明, 可能我真的有點弄混了....謝謝..
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-10-03 08:48:54 IP:147.8.xxx.xxx 未訂閱
The implementation is not shown in the above example... I think it is trivial ..... class="code">procedure TObjectA.MyCall; begin ShowMessage('A'); end; procedure TObjectB.MyCall; begin ShowMessage('B'); end; Note that TObjectA and TObjectB has no direct relationship except they both inherit from TAutoObject and implement the interface IMyInterface.
dhting
一般會員


發表:6
回覆:20
積分:10
註冊:2002-04-15

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-10-03 10:06:48 IP:210.241.xxx.xxx 未訂閱
引言: The implementation is not shown in the above example... I think it is trivial ..... class="code">procedure TObjectA.MyCall; begin ShowMessage('A'); end; procedure TObjectB.MyCall; begin ShowMessage('B'); end; Note that TObjectA and TObjectB has no direct relationship except they both inherit from TAutoObject and implement the interface IMyInterface.
我照著實作後, 程式在(A as IMyInterface).MyCall出現了error 訊息是Operator not applicable to the operand type 著實把我弄糊塗了...
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-10-03 11:08:58 IP:147.8.xxx.xxx 未訂閱
class="code">unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, COMObj; type IMyInterface = interface procedure MyCall; end; TObjectA = class(TInterfacedObject, IMyInterface) procedure MyCall; end; TObjectB = class(TInterfacedObject, IMyInterface) procedure MyCall; end; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.dfm} procedure TObjectA.MyCall; begin ShowMessage('A'); end; procedure TObjectB.MyCall; begin ShowMessage('B'); end; procedure TForm1.Button1Click(Sender: TObject); var IA, IB: IMyInterface; begin IA := TObjectA.Create; IB := TObjectB.Create; IA.MyCall; IB.MyCall; end; end. Note that there is no need in freeing the objects created since they would be destroyed when IA & IB go out of their scope (i.e. exit procedure Button1Click).
dhting
一般會員


發表:6
回覆:20
積分:10
註冊:2002-04-15

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-10-06 10:29:11 IP:210.241.xxx.xxx 未訂閱
引言: class="code">unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, COMObj; type IMyInterface = interface procedure MyCall; end; TObjectA = class(TInterfacedObject, IMyInterface) procedure MyCall; end; TObjectB = class(TInterfacedObject, IMyInterface) procedure MyCall; end; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.dfm} procedure TObjectA.MyCall; begin ShowMessage('A'); end; procedure TObjectB.MyCall; begin ShowMessage('B'); end; procedure TForm1.Button1Click(Sender: TObject); var IA, IB: IMyInterface; begin IA := TObjectA.Create; IB := TObjectB.Create; IA.MyCall; IB.MyCall; end; end. Note that there is no need in freeing the objects created since they would be destroyed when IA & IB go out of their scope (i.e. exit procedure Button1Click).
感謝..我已經試出來了, 雖然還不太懂, 但是有點感覺了.. 我想我再試一些東東也許就可以更明白它的用法
系統時間:2024-04-28 15:44:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!