Code
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//这个类中的两个字段没有封装
TMyClass = class
FName: string;
FAge: Integer;
end;
//这个类中的两个字段封装了,外部不能读写
TMyClass2 = class
private
FName: string;
FAge: Integer;
end;
//那怎么读写?用属性啊!
TMyClass3 = class
private
FName: string;
FAge: Integer;
procedure SetAge(const Value: Integer);
procedure SetName(const Value: string);
published
property Name: string read FName write SetName;
property Age: Integer read FAge write SetAge;
end;
(*
* 现在 TMyClass3 中的两上字段:FName, FAge 和两个方法:SetAge,SetName
* 都被封装在私有区 private 内,不允许外部读写,只能是内部调用。
* 不过,private的封装在本单元内是无效的!
* 现在好了,有了strict 标识符。
*)
TMyClass4 = class
strict private
FName: string;
FAge: Integer;
procedure SetAge(const Value: Integer);
procedure SetName(const Value: string);
published
property Name: string read FName write SetName;
property Age: Integer read FAge write SetAge;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass3 }
procedure TMyClass3.SetAge(const Value: Integer);
begin
if (Value > 0) and (Value < 200) then FAge := Value;
end;
procedure TMyClass3.SetName(const Value: string);
begin
if '' <> Value then FName := Value;
end;
{ TMyClass4 }
procedure TMyClass4.SetAge(const Value: Integer);
begin
if (Value > 0) and (Value < 200) then FAge := Value;
end;
procedure TMyClass4.SetName(const Value: string);
begin
if '' <> Value then FName := Value;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Class3: TMyClass3;
Class4: TMyClass4;
begin
Class3 := TMyClass3.Create; //Class3. 后面可以看到 FName,FAge,SetAge,SetName
Class4 := TMyClass4.Create; //Class4. 后面就看不到 FName,FAge,SetAge,SetName
Class4.Name := 'eboy';
Class4.Age := '99';
Class3.Free;
Class4.Free;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//这个类中的两个字段没有封装
TMyClass = class
FName: string;
FAge: Integer;
end;
//这个类中的两个字段封装了,外部不能读写
TMyClass2 = class
private
FName: string;
FAge: Integer;
end;
//那怎么读写?用属性啊!
TMyClass3 = class
private
FName: string;
FAge: Integer;
procedure SetAge(const Value: Integer);
procedure SetName(const Value: string);
published
property Name: string read FName write SetName;
property Age: Integer read FAge write SetAge;
end;
(*
* 现在 TMyClass3 中的两上字段:FName, FAge 和两个方法:SetAge,SetName
* 都被封装在私有区 private 内,不允许外部读写,只能是内部调用。
* 不过,private的封装在本单元内是无效的!
* 现在好了,有了strict 标识符。
*)
TMyClass4 = class
strict private
FName: string;
FAge: Integer;
procedure SetAge(const Value: Integer);
procedure SetName(const Value: string);
published
property Name: string read FName write SetName;
property Age: Integer read FAge write SetAge;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass3 }
procedure TMyClass3.SetAge(const Value: Integer);
begin
if (Value > 0) and (Value < 200) then FAge := Value;
end;
procedure TMyClass3.SetName(const Value: string);
begin
if '' <> Value then FName := Value;
end;
{ TMyClass4 }
procedure TMyClass4.SetAge(const Value: Integer);
begin
if (Value > 0) and (Value < 200) then FAge := Value;
end;
procedure TMyClass4.SetName(const Value: string);
begin
if '' <> Value then FName := Value;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Class3: TMyClass3;
Class4: TMyClass4;
begin
Class3 := TMyClass3.Create; //Class3. 后面可以看到 FName,FAge,SetAge,SetName
Class4 := TMyClass4.Create; //Class4. 后面就看不到 FName,FAge,SetAge,SetName
Class4.Name := 'eboy';
Class4.Age := '99';
Class3.Free;
Class4.Free;
end;
end.