我自己的实际参数太多 class?
Too many actual parameters for my own class?
我今天在 Delphi 开始使用 OOP。我做了一个简单的 'Box' 函数,当用户输入长度、宽度和高度时 returns 音量。
这是我的 class:
unit clsBox;
interface
uses
SysUtils;
Type
TBox = class(TObject)
private
fL, fB, fH : Integer;
constructor Create (a, b, c : Integer);
function getVolume : Integer;
public
end;
implementation
{ TBox }
constructor TBox.Create(a, b, c: Integer);
begin
a := fL;
b := fB;
c := fH;
end;
function TBox.getVolume: Integer;
begin
Result := fL*fb*fh;
end;
end.
我在原单元的私有部分也为盒子创建了变量
myBox : TBox;
但是当我尝试这个时:
procedure TForm1.btnGetVolumeClick(Sender: TObject);
var
l,b,h : Integer;
begin
l := StrToInt(edtLegth.Text);
b := StrToInt(edtBreadth.Text);
h := StrToInt(edtHeight.Text);
myBox := TBox.Create(l,b,h); //<---- here
end;
它给我一个错误说 Too many actual parameteres
您的构造函数是私有的,因此其他单元看不到。从另一个单元可以看到在 TObject
中声明的无参数构造函数,这就是编译器假定您正在调用的内容。
构造你的构造函数public。
当你想调用getVolume
时,你会遇到同样的问题。也许这是为了用作 属性 getter。
您的构造函数也错误地执行了初始化。所有三个赋值语句都不正确,需要将它们的操作数反转。
构造函数参数的名称不提供信息。 reader 如何从名称 a、b 和 c 推断出它们的用途?
我今天在 Delphi 开始使用 OOP。我做了一个简单的 'Box' 函数,当用户输入长度、宽度和高度时 returns 音量。
这是我的 class:
unit clsBox;
interface
uses
SysUtils;
Type
TBox = class(TObject)
private
fL, fB, fH : Integer;
constructor Create (a, b, c : Integer);
function getVolume : Integer;
public
end;
implementation
{ TBox }
constructor TBox.Create(a, b, c: Integer);
begin
a := fL;
b := fB;
c := fH;
end;
function TBox.getVolume: Integer;
begin
Result := fL*fb*fh;
end;
end.
我在原单元的私有部分也为盒子创建了变量
myBox : TBox;
但是当我尝试这个时:
procedure TForm1.btnGetVolumeClick(Sender: TObject);
var
l,b,h : Integer;
begin
l := StrToInt(edtLegth.Text);
b := StrToInt(edtBreadth.Text);
h := StrToInt(edtHeight.Text);
myBox := TBox.Create(l,b,h); //<---- here
end;
它给我一个错误说 Too many actual parameteres
您的构造函数是私有的,因此其他单元看不到。从另一个单元可以看到在 TObject
中声明的无参数构造函数,这就是编译器假定您正在调用的内容。
构造你的构造函数public。
当你想调用getVolume
时,你会遇到同样的问题。也许这是为了用作 属性 getter。
您的构造函数也错误地执行了初始化。所有三个赋值语句都不正确,需要将它们的操作数反转。
构造函数参数的名称不提供信息。 reader 如何从名称 a、b 和 c 推断出它们的用途?