D
dsuser
Unregistered / Unconfirmed
GUEST, unregistred user!
//delphi实例大全一例
unit TestClass;
interface
type
TTest = class
protected
ProtectedData: Integer;
public
PublicData: Integer;
function GetValue: string;
end;
implementation
uses
SysUtils;
function TTest.GetValue: string;
begin
Result := Format ('Public: %d, Protected: %d',
[PublicData, ProtectedData]);
end;
end.
//////////////////////////////////////////////////////////////////////////////
......
uses
TestClass;
procedure TForm1.Button1Click(Sender: TObject);
var
Obj: TTest;
begin
Obj := TTest.Create;
Obj.PublicData := 10;
// Obj.ProtectedData := 20
//won't compile
ShowMessage (Obj.GetValue);
Obj.Free;
end;
type
TFake = class (TTest);
procedure TForm1.Button2Click(Sender: TObject);
var
Obj: TTest;
begin
Obj := TTest.Create;
Obj.PublicData := 10;
TFake (Obj).ProtectedData := 20
// compiles!
ShowMessage (Obj.GetValue);
Obj.Free;
end;
end.
问:注释掉那行为什么//won't compile??
unit TestClass;
interface
type
TTest = class
protected
ProtectedData: Integer;
public
PublicData: Integer;
function GetValue: string;
end;
implementation
uses
SysUtils;
function TTest.GetValue: string;
begin
Result := Format ('Public: %d, Protected: %d',
[PublicData, ProtectedData]);
end;
end.
//////////////////////////////////////////////////////////////////////////////
......
uses
TestClass;
procedure TForm1.Button1Click(Sender: TObject);
var
Obj: TTest;
begin
Obj := TTest.Create;
Obj.PublicData := 10;
// Obj.ProtectedData := 20
//won't compile
ShowMessage (Obj.GetValue);
Obj.Free;
end;
type
TFake = class (TTest);
procedure TForm1.Button2Click(Sender: TObject);
var
Obj: TTest;
begin
Obj := TTest.Create;
Obj.PublicData := 10;
TFake (Obj).ProtectedData := 20
// compiles!
ShowMessage (Obj.GetValue);
Obj.Free;
end;
end.
问:注释掉那行为什么//won't compile??