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

如何將另一個Form使用類別物件?

尚未結案
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-18 11:34:47 IP:219.81.xxx.xxx 未訂閱
我在Form1裡有一個類別.  
class Profile
{
private:
  String Name;
  int Age;
public:
  void InputData(String name,int age)
    {
     Name=name;
     Age=age;
    }
  void OutputData(void)
    {
     Form1->Label1->Caption=Name;
     Form1->Label2->Caption=Age;
    }
};
 
請問我要如何在Form2使用? 謝謝.
kswedu
一般會員


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-18 12:22:29 IP:211.76.xxx.xxx 未訂閱
試試在Form2 #include "Form1.h"
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-18 16:27:36 IP:163.28.xxx.xxx 未訂閱
並沒有"Form1.h"這個. 我試過在form2上include unit1.h, 產生一個物件Profile abc,這樣還是不行... 不曉得哪裡出錯了?
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-04-18 17:35:54 IP:203.67.xxx.xxx 未訂閱
引言: 並沒有"Form1.h"這個. 我試過在form2上include unit1.h, 產生一個物件Profile abc,這樣還是不行... 不曉得哪裡出錯了?
nlj859你好< >: 試了一下,好像是沒問題的< >! In Unit1.h

~~~
class TForm1 : public TForm
{
__published:        // IDE-managed Components
        TButton *Button1;
        TLabel *Label1;
        TLabel *Label2;
        TButton *Button2;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
private:        // User declarations
public:                // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;    class Profile
{
  private:
           String Name;
           int Age;
  public:
           void InputData(String name,int age)
           {
            Name=name;
            Age=age;
           }               void OutputData(void)
           {
            Form1->Label1->Caption=Name;
            Form1->Label2->Caption=Age;
           }
};    ~~~
In Unit1.cpp

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 Form2->Show();
}
In Unit2.cpp

void __fastcall TForm2::Button1Click(TObject *Sender)
{
 Profile* profile=new Profile;     profile->InputData("我",29);
 profile->OutputData();
}    
嗯,這是我測試的,是ok的,當然要記得Unit1和Unit2互相include彼此的.h檔! 發表人 - m8815010 於 2004/04/18 17:39:07 發表人 - m8815010 於 2004/04/18 17:40:28
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-04-18 18:08:03 IP:163.28.xxx.xxx 未訂閱
恩,謝謝你. 我之前是把那個class放在.cpp裡,現在把他放到.h裡就沒問題了. 雖然說放到.cpp裡的話直接include .cpp就好了.. 但為何一定要放到.h裡面呢? 另外, 請問我建立一個新物件使用  
 
Profile* profile=new Profile;
 profile->InputData("我",29);
 profile->OutputData();
和使用
 
Profile newObj;
newObj.InputData("我",29);
newObj.OutputData();
這兩者的差別在哪? 如果我要以傳物件的方式傳到form2裡使用,那該怎麼寫? ex:Form2有一個receivedObj(物件) 要怎麼去表示呼叫和函數宣告? 謝謝. 發表人 - nlj859 於 2004/04/18 18:16:24 發表人 - nlj859 於 2004/04/18 18:18:09 發表人 - nlj859 於 2004/04/18 19:19:16
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-04-18 21:24:43 IP:203.67.xxx.xxx 未訂閱
引言: 恩,謝謝你. 我之前是把那個class放在.cpp裡,現在把他放到.h裡就沒問題了. 雖然說放到.cpp裡的話直接include .cpp就好了.. 但為何一定要放到.h裡面呢? 另外, 請問我建立一個新物件使用
 
Profile* profile=new Profile;
 profile->InputData("我",29);
 profile->OutputData();
和使用
 
Profile newObj;
newObj.InputData("我",29);
newObj.OutputData();
這兩者的差別在哪? 如果我要以傳物件的方式傳到form2裡使用,那該怎麼寫? ex:Form2有一個receivedObj(物件) 要怎麼去表示呼叫和函數宣告? 謝謝.
nlj859你好: 一個.>>這個新宣告的 class="code"> Form1->Label1->Caption=Name; Form1->Label2->Caption=Age; 另外class宣告在c 中有兩種,一為原始方式,一為指標方式,就如同你寫的那樣,兩種同為標準的寫法,只是指標方式宣告要用delete的指令來release資源,而Builder的VCL視窗元件也都限定用指標方式宣告!嗯,沒辦法說的太清楚,你可以自已查書一下先! 最後,你最後的問題應該都可以用> > >> ~~~<> > <>> <> >>> >!
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-04-18 22:46:56 IP:163.28.xxx.xxx 未訂閱
再問一個問題? 如果是在兩台不同電腦,那該如果include? ex: A電腦有Unit1.h, B電腦是Unit2.h 謝謝.
m8815010
版主


發表:99
回覆:372
積分:289
註冊:2003-11-13

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-04-19 08:12:28 IP:61.63.xxx.xxx 未訂閱
引言: 再問一個問題? 如果是在兩台不同電腦,那該如果include? ex: A電腦有Unit1.h, B電腦是Unit2.h 謝謝.
嗯,歹勢nlj859!這我可能沒辦法了!這樣的經驗我目前沒有,也不知道怎麼做! 但我記得我有看過站上有類似的問題, >!
nlj859
資深會員


發表:139
回覆:375
積分:322
註冊:2004-03-20

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-04-19 13:23:01 IP:163.28.xxx.xxx 未訂閱
恩,我再去找找相關的資料. 非常謝謝你不厭其煩的回答我問題~
系統時間:2024-07-07 21:18:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!