//in this unit,u create a self defined class unit pubshare;interface uses Windows,Messages,SysUtils,Classes,Graphics,Controls, Forms,Dialogs,StdCtrls,Grids,DBGrids,Buttons,ComCtrls,ExtCtrls, comobj;type TShare=class(TObject) public function CheckValidNum(s:string):boolean; //检查字符串是否是合法的数值 constructor Create(); end; implementation var pubfun:TShare; constructor TShare.Create(); begin inherited Create(); end;function TShare.CheckValidNum(s:string):boolean; begin try strtofloat(s); except result:=false; MyShow(s+'不是数值'); exit; end; result:=true; end;//========in the under unit,u can use the self-defined class Unit Test; interface uses Windows,Messages,SysUtils,Classes,Graphics,Controls, Forms,Dialogs,StdCtrls,Grids,DBGrids,Buttons,ComCtrls,ExtCtrls, comobj,pubshare; //u should add the pubshare here
procedure test();
implementation var test:TShare; //define the variable of that classprocedure test() begin //create the instance there test:=TShare.create(nil); //u can use and public method now test.fun(); end;end. So simple ,isn't it?
...... POBJ:=TTest_Paper.Create; end; procedure TForm5.FormClose(Sender: TObject; var Action: TCloseAction); begin POBJ.Free; ...... end; procedure TForm5.InitListView_SJXX; var I,K,J:integer; PartsNode,TMNode:TTreeNode; STR:string; begin WriteStartNum_or_EndNum; TMBH_STR:=''; //删除所有的 TreeView1.Items[0].DeleteChildren; For I:=0 TO Length(POBJ.MODE_ARR)-1 DO begin if trim(POBJ.MODE_ARR[I].BFMC)<>'' then begin PartsNode:=TreeView1.Items.AddChild(TreeView1.Items[0],'第'+NUMB[I]+'部分: '+POBJ.MODE_ARR[I].BFMC+'('+'共计'+IntToStr(POBJ.MODE_ARR[I].BFTL)+'小题, 参考时限'+IntToStr(POBJ.MODE_ARR[I].CKSX)+'分钟)'); For K:=0 To Length(POBJ.MODE_ARR[I].PartXM_arr)-1 do begin if trim(POBJ.MODE_ARR[I].PartXM_arr[K].TXMC)<>'' then TMNode:=TreeView1.Items.AddChild(PartsNode,NUMB[K]+'、'+POBJ.MODE_ARR[I].PartXM_arr[K].TXMC+'('+inttostr(POBJ.MODE_ARR[I].PartXM_arr[K].Start_Num)+'~'+inttostr(POBJ.MODE_ARR[I].PartXM_arr[K].End_Num)+')'); For J:=0 to POBJ.MODE_ARR[I].PartXM_arr[K].TM_List.Count-1 do begin Str:=IntToStr(POBJ.MODE_ARR[I].PartXM_arr[K].Start_Num+J)+'.'+'题目编号:'+Copy(trim(POBJ.MODE_ARR[I].PartXM_arr[K].TM_List.Strings[J]),1,17)+' 题目类型'+OBJ.GetMC(Copy(trim(POBJ.MODE_ARR[I].PartXM_arr[K].TM_List.Strings[J]),1,8),'DM_ZSD',DM.ADODataSet3); TreeView1.Items.AddChild(TMNode,Str); TMBH_STR:=TMBH_STR+''''+Copy(trim(POBJ.MODE_ARR[I].PartXM_arr[K].TM_List.Strings[J]),1,17)+''','; end; end; end; end; TreeView1.FullExpand; end;end.
unit pubshare;interface
uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,
Forms,Dialogs,StdCtrls,Grids,DBGrids,Buttons,ComCtrls,ExtCtrls,
comobj;type
TShare=class(TObject)
public
function CheckValidNum(s:string):boolean; //检查字符串是否是合法的数值
constructor Create();
end;
implementation
var
pubfun:TShare;
constructor TShare.Create();
begin
inherited Create();
end;function TShare.CheckValidNum(s:string):boolean;
begin
try
strtofloat(s);
except
result:=false;
MyShow(s+'不是数值');
exit;
end; result:=true;
end;//========in the under unit,u can use the self-defined class Unit Test;
interface
uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,
Forms,Dialogs,StdCtrls,Grids,DBGrids,Buttons,ComCtrls,ExtCtrls,
comobj,pubshare; //u should add the pubshare here
procedure test();
implementation
var
test:TShare; //define the variable of that classprocedure test()
begin
//create the instance there
test:=TShare.create(nil);
//u can use and public method now
test.fun();
end;end.
So simple ,isn't it?
但我以前听同事说,说我的编程是"硬编码",要我学会"软编码",从上看来,实际上就是把过程,函数写在一个单独的unit中,本质上而言,与作成dll或者集中放到一个DataMould里一样.
同事还说,把程序封装成类不论是改写还是维护等,都会非常方便,轻松.
那么,请教 sun_yzh ,类还主要常被程序员用在哪些方面呢? 请指教!
听不懂意思呀!
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Dialogs,DB,ADODB,U_Des,Forms,ComCtrls,Grids,Word2000, OleServer,comobj,U_public;Const SJLX='行政职业能力'; //试卷类型
//定义对试题操作状态的枚举类型数据
type problem_operate=(Insert,Update,Delete,Brows);type
TPartsXM = Class
TXDM:string[8]; // 题型代码
TXMC:String[30]; // 题型名称
TMSL:integer; // 数量
TXZF:integer; // 总分
SX:integer; // 时限
TXSM:string; //题目说明
Start_Num:integer;
End_Num:integer;
TM_List:TStringList;
public {}
constructor Create;
destructor Destroy; Override;
end;type
TTest_ModeXM = class
MBBH:integer; //对应编号
BFBM:String[8]; //部分编码
BFMC:string[255]; //部分名称
BFTL:byte; //部分题量
CKSX:byte; //部分参考时限(单位分钟)
PartXM_arr:Array of TPartsXM; //部分题型
Start_Num:integer;
End_Num:integer;
SFMB:Boolean; // 是否模板 public {重载销毁函数 }
Destructor Destroy;Override; //销毁函数
procedure CreatePartXM_arr(Count:byte);
procedure FreePartXM_arr;
procedure DeletePartXm(Index:integer);
procedure InsertPartXm(Index:integer);
procedure AppendPartXm;
procedure EditPartXm(Index:integer; TXDM:string;TXMC:String;TMSL:integer;TXZF:integer;SX:integer;TXSM:string; StartN,EndN:integer);
procedure SaveToParts_XMData(parts_ID: integer;Commad: TADoCommand; OPT: problem_operate);
end;type
TTest_Paper = class(TObject)
SJBH: integer; // 试卷编号
SJMC: string[60]; //试卷名称
KSSJ: string[10]; //考试日期
KSDQM: string[6]; //考试地区代码
KSDQ: string; //考试地区
KSBM: string[30]; //考试编号
SX: integer; //时限
MF: byte; //试卷总分
TL: integer; // 题量
NR: TMemoryStream; // 试卷内容
MODE_ARR: array of TTest_ModeXM; //相关题型细目
private
{ Private declarations }
Function GetKSDQ(KSDQDataSet_Mchine:TADoDataset):string;
Function GetMC(DM:string; KSDQDataSet_Mchine:TADoDataset):string;
public
{ Public declarations }
Destructor Destroy;Override; //销毁函数
procedure InitSJ_Arr(Count:Byte);
procedure FreeSJ_Arr;
procedure InsertModeObject(index:integer);
procedure AppendModeObject;
procedure DeleteModeObject(index:byte);
procedure EditModeObject(index:byte;BFBM:string;BFMC:string;BFTL:Byte;CKSX:byte; startN,EndN:integer);
procedure SaveTestPaper_infor(Commad:TADoCommand; OPT:problem_operate);
procedure SaveMBToDataBase(Commad:TADoCommand; OPT:problem_operate);
Procedure SaveMBXMToDataBase(BH, ID: integer;Commad: TADoCommand; OPT: problem_operate);
procedure SaveTestPaperXM(BH, ID: integer;Commad: TADoCommand; OPT: problem_operate);
end;implementation
請再把
實現方法,以及在delphi中如何引用這個類搞點代碼讓我看看/
別嫌分少,結帖子時我再另外開帖子加50分,說話絕對算數!
U_Test_paper,......;type
TFm_SetSJ= class(TForm)
...
private
POBJ:TTest_Paper;
end;implementation
uses
{$R *.dfm}procedure TForm5.FormCreate(Sender: TObject);
begin
...... POBJ:=TTest_Paper.Create;
end;
procedure TForm5.FormClose(Sender: TObject; var Action: TCloseAction);
begin
POBJ.Free;
......
end;
procedure TForm5.InitListView_SJXX;
var
I,K,J:integer;
PartsNode,TMNode:TTreeNode;
STR:string;
begin
WriteStartNum_or_EndNum;
TMBH_STR:='';
//删除所有的
TreeView1.Items[0].DeleteChildren; For I:=0 TO Length(POBJ.MODE_ARR)-1 DO
begin
if trim(POBJ.MODE_ARR[I].BFMC)<>'' then
begin
PartsNode:=TreeView1.Items.AddChild(TreeView1.Items[0],'第'+NUMB[I]+'部分: '+POBJ.MODE_ARR[I].BFMC+'('+'共计'+IntToStr(POBJ.MODE_ARR[I].BFTL)+'小题, 参考时限'+IntToStr(POBJ.MODE_ARR[I].CKSX)+'分钟)');
For K:=0 To Length(POBJ.MODE_ARR[I].PartXM_arr)-1 do
begin
if trim(POBJ.MODE_ARR[I].PartXM_arr[K].TXMC)<>'' then
TMNode:=TreeView1.Items.AddChild(PartsNode,NUMB[K]+'、'+POBJ.MODE_ARR[I].PartXM_arr[K].TXMC+'('+inttostr(POBJ.MODE_ARR[I].PartXM_arr[K].Start_Num)+'~'+inttostr(POBJ.MODE_ARR[I].PartXM_arr[K].End_Num)+')');
For J:=0 to POBJ.MODE_ARR[I].PartXM_arr[K].TM_List.Count-1 do
begin
Str:=IntToStr(POBJ.MODE_ARR[I].PartXM_arr[K].Start_Num+J)+'.'+'题目编号:'+Copy(trim(POBJ.MODE_ARR[I].PartXM_arr[K].TM_List.Strings[J]),1,17)+' 题目类型'+OBJ.GetMC(Copy(trim(POBJ.MODE_ARR[I].PartXM_arr[K].TM_List.Strings[J]),1,8),'DM_ZSD',DM.ADODataSet3);
TreeView1.Items.AddChild(TMNode,Str);
TMBH_STR:=TMBH_STR+''''+Copy(trim(POBJ.MODE_ARR[I].PartXM_arr[K].TM_List.Strings[J]),1,17)+''',';
end;
end;
end;
end;
TreeView1.FullExpand;
end;end.
TTest_ModeXM 這個類的實現方法!特別是關系到數據庫操作的那一些地方!
POBJ.FreeSJ_Arr;来创建和释放TTest_MODEXM实例
http://community.csdn.net/Expert/topic/4196/4196140.xml?temp=.8409693
接分,我等你接分很久了.
在
http://community.csdn.net/Expert/topic/4196/4196140.xml?temp=.3516046
給你分