|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
优点:简单易学、开发速度快、有很多年“历史”,能找到非常多别人做好的程序来用、配合activeX功能强大,很多php做不到的asp+activeX能做到,例如银行安全控件application|windowInthedatabaseapplicationdevelopment,theprogrammershouldoftendealwithonethingthatiswhentheusercloseawindow(calledForminDelphi)wheredatawasmaintained,theprogramshouldjudgewhetherthedatawaschangedwithoutsavingandwarntheuseraboutthiscase.Almostalltheprogrammercandealwiththiseasily,andthecodeisverysimpleasweknow.Thefollowingcode(writteninDelphi6)isthenormalmethodtodealwiththeproblem.ItisbaseononetablemaintenanceandthereareaDBGrid(namedDBGRid)whichshowsthedataofthetable,aDBEdit(namedDBEditName)whichcanbeusedtoeditthedataofthetableandsixButtons(respectivenamedBtnAdd,BtnModify,BtnDelete,BtnSave,BtnCancle,BtnClose).ThelastbuttonisusedtoclosetheFormandtheothersaredealingwiththedata.Ithinktheirnameshowtheirfunctionclearlyandnoexplanationisgivenhere.
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,ExtCtrls,StdCtrls,Buttons,Grids,DBGrids;
type
TForm1=class(TForm)
BtnModify:TBitBtn;
BtnCancel:TBitBtn;
BtnAdd:TBitBtn;
BtnDelete:TBitBtn;
BtnSave:TBitBtn;
BtnClose:TBitBtn;
DBGrid1:TDBGrid;
procedureFormCloseQuery(Sender:TObject;varCanClose:Boolean);
procedureBtnSaveClick(Sender:TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
protected
{Protecteddeclarations}
functionDataModifiedWithoutSaving():Boolean;virtual;abstract;
end;
varForm1:TForm1;
implementation
{$R*.dfm}
procedureTForm1.FormCloseQuery(Sender:TObject;
varCanClose:Boolean);
begin
ifDataModifiedWithoutSavingthen
begin
ifMessageDlg(Thedatahasbechangedwithoutsaving,wouldyoulikesavethedata?,mtWarning,[mbYes,mbNo],0)=mrNothen
begin
CanClose:=True;
endelsebegin
BtnSaveClick(Self);
CanClose:=True;
end;
end;
end;
procedureTForm1.BtnSaveClick(Sender:TObject);
begin
//
end;
end.
Theabovecodeisnotverygood(maybeyoucangiveamoreeffectone),butitreallycandealwiththeproblemwementionedatthebeginningofthepaper.Butletushaveathoughtnow;ifwegottwentyformsweshouldcopythecodeoftheFormCloseQueryfunctiontwentytimes.Doyouthinkitisaboringthing?Maybenot,buthowaboutthattheteamleaderdonotthinkthecodeyouhavewrittenisverygoodandhegivesyouanotherone?Thencopyagain?Ithinkitisneededtofindaneffectmethodtodealwiththisproblem.ThetoolIshowtheexamplehereisDelphi,ofcoursemanyothertoolsareusedinthesoftwarefieldsuchasVirsualC++,VirsualBasicandsoon.Butmostofthem(almostallofthem)arethetoolswhichsupportOOP(object-oriented
programming).ThethreemainpropertyofOOPareEncapsulation,InheritanceandDynamic.WecanusetheDynamicpropertytodealwiththeproblem.Todoso,weneedaformwhichhastheFormCloseQueryfunctiontobetheparentformofthosewherethedataismaintained.NowyoumayaskmeaquestionthatishowabouttheDataModifiedWithoutSavingfunction,thisfunctionhastobedifferentineveryform,andtheprogramshouldcalltheoneinthechildFormnottheoneintheparentForm.Yes,itisabsolutelyrightanditisjustwhattheDynamicdoes.WhenusingDelphi,thevirtualanddynamicmethodsshowthepropertyofDynamic.Virtualanddynamicmethods,unlikestaticmethods,canbeoverriddenindescendantclasses.Whenanoverriddenmethodiscalled,theactual(runtime)typeoftheclassorobjectusedinthemethodcallDnotthedeclaredtypeofthevariableDdetermineswhichimplementationtoactivate.[1]Dorememberthatthevirtualisneededhere,becausetheDelphiacceptthemethodtobestaticdefault.[1]
FunctionDataModifiedWithoutSaving():Boolean;virtual;abstract;
Anotherthingwehavetodoismoveitfromprivateparttotheprotectedpart,sincetheprivatepartcannotaccessoutoftheclass.AndIthinktheimplementationpartofthefunctionisuseless,soIdeletethispartanddeclarethef</p>大家可以自己去看一看.可以说看得想呕吐.以前有次下了个动网来看.里面连基本内置函数的保护措施(函数没防御性)都没有.难怪经常补这个补那个了.可能现在.NET版会好点吧 |
|