【发布时间】:2017-02-06 17:28:59
【问题描述】:
我正在学习 Delphi 和 Lazarus 中的教程。我正在使用 Delphi 10.1 Berlin Update 2 和 Lazarus 1.6.2。
以下构造函数在 Delphi 中有效,但 TDog 类中的构造函数在 Lazarus 中失败并出现“重复标识符”错误。
我搜索过的所有教程和论坛看起来都应该没有问题。
unit Animal;
interface
uses
classes;
type
TAnimal = class
private
FName: String;
FBrain: Integer;
FBody: Integer;
FSize: Integer;
FWeight: Integer;
public
constructor create(Name: String; Brain, Body, Size, Weight: Integer);
procedure Eat; virtual;
property Name: String read FName;
property Brain: Integer read FBrain;
property Body: Integer read FBody;
property Size: Integer read FSize;
property Weight: Integer read FWeight;
end;
implementation
constructor TAnimal.create(Name: String; Brain, Body, Size, Weight: Integer);
begin
FName:= Name;
FBrain:= Brain;
FBody:= Body;
FSize:= Size;
FWeight:= Weight;
end;
procedure TAnimal.Eat;
begin
Writeln('TAnimal.eat called');
end;
end.
unit Dog;
interface
uses
classes,
Animal;
type
TDog = class (TAnimal)
private
FEyes: Integer;
FLegs: Integer;
FTeeth: Integer;
FTail: Integer;
FCoat: String;
procedure Chew;
public
constructor create(Name: String; Size, Weight, Eyes, Legs,
Teeth, Tail: integer; Coat: String);
procedure Eat; override;
end;
implementation
//following fails in Lazarus
constructor TDog.create(Name: String; Size, Weight, Eyes, Legs,
Teeth, Tail: integer; Coat: String);
//this works, changing implementation also
//constructor Create(aName: String; aSize, aWeight, Eyes, Legs,
// Teeth, Tail: integer; Coat: String);
begin
inherited Create(Name, 1, 1, Size, Weight);
FEyes:= Eyes;
FLegs:= Legs;
FTeeth:= Teeth;
FTail:= Tail;
FCoat:= Coat;
end;
procedure TDog.Chew;
begin
Writeln('TDog.chew called');
end;
procedure TDog.Eat;
begin
inherited;
Writeln('TDog.eat called');
chew;
end;
end.
【问题讨论】:
-
由于你是用不同的参数复制相同的构造函数名称,你需要
reintroduce构造函数,否则使用{$mode delphi}。 -
感谢您的回复。我尝试了 {$mode delphi} 并且成功了。 reintroduce 没有任何效果,也不允许使用类名(Dog)作为实例变量。尽管在 Delphi 中一切正常,但从现在开始我将保持一切都是独一无二的。在 Java 和 C# 中,在类名之后以小写形式命名类实例变量似乎很常见,因为语言区分大小写。