在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.登不了大雅之堂地理解几个概念
代码
procedure TForm1.Button1Click(Sender: TObject);
var ACaption:String; begin ACaption:=TButton(Sender).Caption;//Sender从TObject转化到TButton ShowMessage(Format('You clicked ''%s'' !',[ACaption])); end;
代码
procedure TForm1.Button1Click(Sender: TObject);
var S_Str:String; P_Str:PChar; begin S_Str:='I love China!'; P_Str:=PChar(S_Str); S_Str:=''; S_Str:=String(P_Str); ShowMessage(S_Str); end;
但是在面对对象的程序设计过程中,强调的是安全性,如(一)的强制类型转化存在着不安全性。如下的代码,依然是写Button1.OnClick事件:
var
StrLst:TStrings; begin StrLst:=TStrings.Create; StrLst.Add('I love Japan!'); StrLst.Free; end;
var
StrLst:TStrings; begin StrLst:=TStringList.Create;//借助其子类的构造器,对StrLst进行子类化 StrLst.Add('I love China!'); StrLst.Free; end;
(三)
var
StrLst:TStringList; begin StrLst:=TStringList.Create; file://放弃吧,不要再用抽象类,完全用它的“儿子”来你的事吧 StrLst.Add('I love China!'); StrLst.Free; end;
代码
unit Unit2;
interface type TEmployee=class private FName:String; public Constructor Create; function GetName:String; procedure SetName(AName:String); end; implementation { TEmployee } constructor TEmployee.Create; begin FName:='BlazingFire'; end; function TEmployee.GetName: String; begin Result:=FName; end; procedure TEmployee.SetName(AName: String); begin FName:=AName; end; end.
如上代码,我们就用了一个过程SetName和一个函数GetName对私有变量FName进行完全的封装。我们要对FName操作就只有这样:
代码
uses
unit2; procedure TForm1.Button1Click(Sender: TObject); var AEmployee:TEmployee; begin AEmployee:=TEmployee.Create; AEmployee.SetName('Rose');//利用SetName来设置FName MessageBox(Handle,PChar(AEmployee.GetName),'Empoyee',0); file://用GetName来访问FName AEmployee.Free; end;
代码
unit Unit2;
interface type TDivision=Class public file://多态性让你的程序更据有“柔韧性” function GetDiv(Num1,Num2:Double):Double;overload; function GetDiv(Num1,Num2:integer):integer;overload; end; implementation { Division } function TDivision.GetDiv(Num1, Num2: Double): Double; begin try Result:=Num1/Num2; except Result:=0;//提供弹形处理机制,处理除数为0情况 end; end; function TDivision.GetDiv(Num1, Num2: integer): integer; begin try Result:=Num1 div Num2; except Result:=0;//提供弹形处理机制,处理除数为0情况 end; end; end.
代码
uses
unit2; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var Division:TDivision; IValue:integer; FValue:Double; begin Division:=TDivision.Create; IValue:=Division.GetDiv(1,2); FValue:=Division.GetDiv(1.0,2); IValue:=Division.GetDiv(1,0); FValue:=Division.GetDiv(1.0,0); Division.Free; end;
6.类是一种代码重用机制
代码
unit Unit2;
interface type TDivision=Class public function GetDiv(Num1,Num2:Double):Double;overload; function GetDiv(Num1,Num2:integer):integer;overload; end; type TOperation=Class(TDivision) public function GetAdd(Num1,Num2:Double):Double; end; implementation { Division } function TDivision.GetDiv(Num1, Num2: Double): Double; begin try Result:=Num1/Num2; except Result:=0; end; end; function TDivision.GetDiv(Num1, Num2: integer): integer; begin try Result:=Num1 div Num2; except Result:=0; end; end; { TOperation } function TOperation.GetAdd(Num1, Num2: Double): Double; begin Result:=Num1+Num2; end; end.
这里我们从TDivision继承了一个子类TOperation。TOperation就可以即有TDivsion公有方法GetDiv,又有自己的独特的方法GetAdd。这是类为我们提供的“鱼和熊掌兼得”之法。不错吧。:) |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论