用過VB的人都知道,可以在工程中增加類模快來存放共用方法,而在delphi中如何也能與VB一樣存放自己的類呢?通過下面的講解,我想你一定會有所收獲的。
一、在工程中增加一個庫單元
單擊菜單的順序為 File -> New -> Unit 這樣就為你的工程增加了一個庫單元。新增加的庫單元內(nèi)容如:
unit global;//庫單元的名字
interface
file://<---這里加入選擇性庫單元列表
implementation
end.
二、在庫單元中增加自己的類
在Object Pascal中,用關(guān)鍵字Class來聲明類。使用如下語法:
Type
CTestclass = class file://定義一個類,命名規(guī)律自己看一看delphi相關(guān)的命名規(guī)律
end;
當(dāng)然,這段代碼,沒有什么實際用途,只是僅僅聲明了一個空類,而類在沒有任何的數(shù)據(jù)和*作,在下面我們可以向類中添加數(shù)據(jù)和方法。
Type
CTestclass = class
Tmessage:String;
Procedure SetText(text:String);
Function GetText:String;
end;
類的函數(shù)成員和過程成員成為類的方法。他們的說明和定義方法與普通的函數(shù)和過程相似,的區(qū)別是要在函數(shù)名和過程名前面加類名和句點。
Procdeure CTestclass.SetText(text:String);
Begin
Tmessage:=text;
end;
Function CTestclass.GetText:String;
Begin
GetText:=Tmessage;
end;
這樣一個簡單的類就編寫完成了,你可以按下面所講的步驟進(jìn)行調(diào)用。將上面的代碼整理一下,這個庫單元的完整代碼如下:
unit global;//庫單元的名字
interface file://接口部分
uses
windows;//需要引用的其它庫單元列表
Type file://接口類型定義
CTestclass = class
Tmessage:String;
Procedure SetText(text:String);
Function GetText:String;
end;
implementation
Procdeure CTestclass.SetText(text:String);
Begin
Tmessage:=text;
end;
Function CTestclass.GetText:String;
Begin
GetText:=Tmessage;
end;
end.
一、在工程中增加一個庫單元
單擊菜單的順序為 File -> New -> Unit 這樣就為你的工程增加了一個庫單元。新增加的庫單元內(nèi)容如:
unit global;//庫單元的名字
interface
file://<---這里加入選擇性庫單元列表
implementation
end.
二、在庫單元中增加自己的類
在Object Pascal中,用關(guān)鍵字Class來聲明類。使用如下語法:
Type
CTestclass = class file://定義一個類,命名規(guī)律自己看一看delphi相關(guān)的命名規(guī)律
end;
當(dāng)然,這段代碼,沒有什么實際用途,只是僅僅聲明了一個空類,而類在沒有任何的數(shù)據(jù)和*作,在下面我們可以向類中添加數(shù)據(jù)和方法。
Type
CTestclass = class
Tmessage:String;
Procedure SetText(text:String);
Function GetText:String;
end;
類的函數(shù)成員和過程成員成為類的方法。他們的說明和定義方法與普通的函數(shù)和過程相似,的區(qū)別是要在函數(shù)名和過程名前面加類名和句點。
Procdeure CTestclass.SetText(text:String);
Begin
Tmessage:=text;
end;
Function CTestclass.GetText:String;
Begin
GetText:=Tmessage;
end;
這樣一個簡單的類就編寫完成了,你可以按下面所講的步驟進(jìn)行調(diào)用。將上面的代碼整理一下,這個庫單元的完整代碼如下:
unit global;//庫單元的名字
interface file://接口部分
uses
windows;//需要引用的其它庫單元列表
Type file://接口類型定義
CTestclass = class
Tmessage:String;
Procedure SetText(text:String);
Function GetText:String;
end;
implementation
Procdeure CTestclass.SetText(text:String);
Begin
Tmessage:=text;
end;
Function CTestclass.GetText:String;
Begin
GetText:=Tmessage;
end;
end.