【发布时间】:2015-08-20 03:06:03
【问题描述】:
我正在尝试在 Free Pascal 中的类声明中定义所有类方法,但我无法在网上找到任何示例。目前我必须这样做:
unit Characters;
{$mode objfpc}{$H+}
// Public
interface
type
TCharacter = class(TOBject)
private
FHealth, FAttack, FDefence: Integer;
procedure SetHealth(newValue: Integer);
public
constructor Create(); virtual;
procedure SayShrek();
function GetHealth(): Integer;
published
property Health: Integer read GetHealth write SetHealth;
end;
// Private
implementation
constructor TCharacter.Create;
begin
WriteLn('Ogres have LAYERS!');
end;
procedure TCharacter.SayShrek;
begin
WriteLn('Shrek!');
end;
procedure TCharacter.SetHealth(newValue: Integer);
begin
FHealth:= FHealth + newValue;
end;
function TCharacter.GetHealth() : Integer;
begin
GetHealth:= FHealth;
end;
end.
有没有什么办法可以让这更干净一点?在别处定义所有内容看起来很杂乱无章。
编辑:
为了澄清,我想按照以下方式做一些事情:
TMyClass = class(TObject)
public
procedure SayHi();
begin
WriteLn('Hello!');
end;
end;
而不必进一步定义它。这可能吗?
【问题讨论】:
标签: pascal lazarus freepascal delphi