Delphi 封装-属性的封装
Posted yishen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi 封装-属性的封装相关的知识,希望对你有一定的参考价值。
type TMan = class(TObject) public FAge: integer; end;
type TMan = class(TObject) private FAge: Integer; function GetAge: integer; procedure SetAge(Value: integer); public property Age: integer read GetAge write SetAge; end;
procedure TMan.SetAge(Value: integer); begin if(Value > 0) and (Value < 120) then FAge := Value; end;
procedure TMan.SetPhoto(value: TPicture); begin if FPhoto.Picture = value then exit; Repaint; end;
unit Unit1; uses //... type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private FForm:TForm; public property Form: TType read FForm write FForm; end; var Form1: TForm1; implementation procedure TForm1.FormCreate(Sender: TObject); begin Form2 := TFrom2.create(self); end; procedure TFrom1.Button1Click(Sender: TObject); begin if Assigned(FForm) then TForm2(FForm).Shoe;//访问的是内部成员FForm,主要FForm需要转型. end; procedure TFrom1.Button2Click(Sender: TObject); begin self.caption := ‘Hi‘;//在TForm1类中实用self代替Form1. end; //一下是项目文件中的内容 program Project1; uses Forms, unit1 in ‘Unit1.pas‘{Form1}, Unit2 in ‘Unit2.pas‘{Form2}; begin Application.Initialize; Application.CreateForm(TForm1,Form1); Application.CreateForm(TForm2,Form2); //通过属性传递Form2的引用 Form1.FForm := Form2; Application.Run; end; type TMan = class(TObject) private FAge: Integer; function GetAge: integer; procedure SetAge(Value: integer); public property Age: integer read GetAge write SetAge; end; procedure TMan.SetAge(Value: integer); begin if(Value > 0) and (Value < 120) then FAge := Value; end; procedure TMan.SetPhoto(value: TPicture); begin if FPhoto.Picture = value then exit; Repaint; end; unit uCreateManClass; interface uses Dialogs; type TSkinColor = (scWhite, scYellow, scBlack, scDark); TMan = class(TObject) private FAge: integer; FLanguage: string; FMale: boolean; FName: string; FSkinColor: TSkinColor; FValidData: boolean; function GetAge: integer; function GetColor: TSkinColor; function GetLanguage: string; function GetMale: boolean; function GetName: string; function SetAge(value: integer) function SetColor(value: TSkinColor); procedure SetLanguage(valude: string;); procedure SetMale(valude: boolean); procedure SetName(value: string); public function retrieve:Boolean; function save:boolean; procedure SayHello(Words: pchar); property Age: TType read FAge write FAge; property Language: TType read FLanguage write FLanguage; property Male: TType read FMale write FMale; property Name: TType read FName write FName; property SkinColor: TType read FSkinColor write FSkinColor; property ValidData: TType read FValidData write FValidData; end; implementation function TMan.GetAge: integer; begin result := FAge; end; function TMan.GetColor: TSkinColor; begin result := FSkinColor; end; function TMan.GetLanguage: string; begin result := FLanguage; end; function TMan.GetMale: boolean; begin result := FMale; end; function TMan.retrieve: boolean; begin end; function TMan.save: boolean; begin end; procedure TMan.SayHello(words: pchar); begin ShowMessage(words); end; procedure TMan.SetAge(value: integer) begin if(value > 0) and (value < 120) then FAge := value else FValidData := False; end; procedure TMan.SetColor(Value: TSkinColor); begin if(value <> FSkinColor) then FSkinColor := value; end; procedure TMan.SetLanguage(value: string) begin if(value <> FLanguage)then FLanguage := value; end; procedure TMan.SetMale(value: boolean); begin if(value <> FMale) then FMale := value; end; procedure TMan.SetName(value: string); begin if(value <> FName) then FName := valude; end;
以上是关于Delphi 封装-属性的封装的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装
VSCode自定义代码片段14——Vue的axios网络请求封装