用過vb的人都知道,可以在工程中增加類模快來存放共用方法,而在delphi中如何也能與vb一樣存放自己的類呢?通過下面的講解,我想你一定會有所收穫的。
一,在工程中增加乙個庫單元
單擊選單的順序為 file -> new -> unit 這樣就為你的工程增加了乙個庫單元。新增加的庫單元內容如:
unit global;//庫單元的名字
inte***ce
file://<---這裡加入選擇性庫單元列表
implementation
end.
二,在庫單元中增加自己的類
在object pascal中,用關鍵字class來宣告類。使用如下語法:
type
ctestclass = class file://定義乙個類,命名規律自己看一看delphi相關的命名規律
end;
當然,這段**,沒有什麼實際用途,只是僅僅宣告了乙個空類,而類在沒有任何的資料和操作,在下面我們可以向類中新增資料和方法。
type
ctestclass = class
tmessage:string;
procedure settext(text:string);
function gettext:string;
end;
類的函式成員和過程成員成為類的方法。他們的說明和定義方法與普通的函式和過程相似,唯一的區別是要在函式名和過程名前面加類名和句點。
procdeure ctestclass.settext(text:string);
begin
tmessage:=text;
end;
function ctestclass.gettext:string;
begin
gettext:=tmessage;
end;
unit global;//庫單元的名字
inte***ce 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.
三,呼叫自定義庫單元檔案(或其它庫單元)中的方法
在你需要引用的檔案uses處,新增你自己的庫單元的名稱
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, global; file://注意這裡的global是你自己寫的庫單元的名稱
一旦在uses部分引用了你的庫單元,就可以按如下進行呼叫:
vartclass:ctestclass;
這樣一來就可以如當前檔案中的窗體類一樣呼叫了。完整**如下:
unit unit1;
inte***ce
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, global;
type
tform1 = class(tform)
private
public
end;
varform1: tform1;
tclass:ctestclass; file://你要增加的類的引用宣告
implementation
procedure tform1.formcreate(sender: tobject);
begin
tclass.create;
tclass.settext('這是乙個類的測試');
showmessage(tclass.gettext); file://此處是對你自己寫的類的乙個測試
end;
end.
好了,在你的計算機中輸入完上面的**後,執行試一試吧。這裡只是對庫單元的引用舉了乙個簡單例子,關於詳細寫法請參看相關書籍中對庫單元及類程式設計的講述。
如何在自己的opengl程式中使用GLSL
glsl,即gl shading language,用自己寫的程式取代opengl的固定渲染管線的一種語言。之前看了下rendermonkey,雖然功能不算弱,但使用rendermonkey卻不知道在裡面怎麼用自己的shader去渲染自己的opengl程式,所以就去找下了關於把shader鏈結到自己...
如何在linux程式中使用printk
from 要將linux核心的帶級別控制的printk內容列印出來,在命令列 輸入 dmesg n 8 就將所有級別的資訊都列印出來 linux命令 dmesg 功能說明 顯示開機資訊。語 法 dmesg cn s 緩衝區大小 補充說明 kernel會將開機資訊儲存在ring buffer中。您若是...
android 如何在自己的專案中使用 MVP
官方給我們寫了一些mvp的樣例工程,用不同的概念和工具實現同乙個todo專案。github位址 雖然在官方推出這套mvp開源用例之前,網路上也有很多優秀的開源專案教大家如何使用mvp模式,如果你之前沒看過,其實現在還有乙個好處,直接按官方的來做就是了 官方一出馬,其他的類似專案就啞火了 我看了一下官...