读书人

This form of method call only allow

发布时间: 2012-08-31 12:55:03 作者: rapoo

This form of method call only allowed in methods of derived types
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClick(Sender: TObject);
private
Persons:array[1..4] of TPerson;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Persons[1]:TBoss.Create('赵晓光',800.0);
Persons[2]:TEmployee.Create('钱明',400.0,3.0,150);
Persons[3]:TPieceWorker.Create('孙正',2.5,200);
Persons[4]:THourlyWorker.Create('李大',13.75,50.0);
end;

procedure TForm1.FormDestory(Sender:TObject);
var
i:Integer;
begin
for i:=1 to 4 do begin
Persons[i].Free;
Persons[i]:=nil;
end;
end;

procedure TForm1.FormClick(Sender: TObject);
var
i:Integer;
begin
for i:=1 to 4 do
ListBox1.Items.Add(Format('%8s %10.2f',[Person[i].Name,Person[i].Earnings]));
end;

end.


unit untPerson;

interface

type
TPerson=class
private
FName:string;
public
constructor Create(NameValue:string);
function Earnings:Real;virtual;abstract;
property Name:string read FName write FName;
end;

implementation

constructor TPerson.Create(NameValue:string);
begin
FName:=NameValue;
end;
end.

type
TBoss=class(TPerson)
private
FSalary:Real;
public
constructor Create(NameValue:string;SalaryValue:Real);
function Earnings:Real;override;
property WeeklySalary:Real read FSalary write FSalary;
end;
implementation

uses
SysUtils;

constructor Create(NameValue:string;SalaryValue:Real);
begin
inherited Create(NameValue);
WeeklySalary:=SalaryValue;
end;

function TBoss.Earnings:Real;
begin
Result:=WeeklySalary;
end;
end.


unit untPieceWork;

interface

uses
untPerson;

type
TPieceWorker=class(TPerson)
private
FQuantity:Integer;
FWagePerPiece:Real;
public
constructor Create(NameValue:string;WagePerPieceValue:Real;QuantityValue:Integer);
function Earnings:Real;override;
property WagePerPiece:Real read FWagePerPiece write FWagePerPiece;
property Quantity:Real read FQuantity write FQuantity;
end;

implementation

constructor Create(NameValue:string;WagePerPieceValue:Real;QuantityValue:Integer);
begin
inherited Create(NameValue);
WagePerPiece:=WagePerPieceValue;
Quantity:=QuantityValue;
end;

function TPieceWorker.Earnings:Real;
begin
Result:=WagePerPiece*Quantity;
end;
end.

unit untEmployee;

interface

uses
untPerson;

type
TEmployee=class(TPerson)
private
FQuantity:Integer;
FWagePerItem:Real;
FSalary:Real;
public
constructor Create(NameValue:string;SalaryValue,WageperItemValue:Real;QuantityValue:Integer);
function Earnings:Real;override;


property WeeklySalary:Real read FSalary write FSalary;
property WagePerItem:Real read FWagePerItem write FWagePerItem;
property Quantity:Integer read FQuantity write FQuantity;
end;

implementation

constructor Create(NameValue:string;SalaryValue,WageperItemValue:Real;QuantityValue:Integer);
begin
inherited Create(NameValue);
WeeklySalary:=SalaryValue;
WageperItem:=WageperItemValue;
Quantity:=QuantityValue;
end;

function TEmployee.Earnings:Real;
begin
Result:=WeeklySalary+WagePerItem*Quantity;
end;
end.


unit untHourlyWorker;

interface

uses
untPerson;

type
THourlyWorker=class(TPerson)
private
FHourWorked:Real;
FWage:Real;
public
constructor Create(NameValue:string;WageValue,HourWorkedValue:Real);
function Earnings:Real;override;
property Wage:Real read FWage write FWage;
property HourWorked:Real read FHourWorked write FHourWorked;
end;

implementation

constructor Create(NameValue:string;WageValue,HourWorkedValue:Real);
begin
inherited Create(NameValue);
Wage:=WageValue;
HourWorked:=HourWorkedValue;
end;

function THourlyWorker.Earnings:Real;
begin
if HoursWorked<=40 then
Result:=Wage*HoursWorked;
else
Result:=Wage*40+Wage*1.5*(HoursWorked-40);
end;
end.

运行时错误提示如下:
[Error] untBoss.pas(22): Undeclared identifier: 'Create'
[Error] untBoss.pas(24): This form of method call only allowed in methods of derived types
[Error] untBoss.pas(24): Not enough actual parameters
[Error] untBoss.pas(25): Undeclared identifier: 'WeeklySalary'
[Error] untBoss.pas(13): Unsatisfied forward or external declaration: 'TBoss.Create'
[Fatal Error] Project1.dpr(9): Could not compile used unit 'untBoss.pas'


[解决办法]
在implementation下,Create函数头部应当加上类名:
constructor 类名.Create
比如constructor THourlyWorker.Create(NameValue:string;WageValue,HourWorkedValue:Real);

[解决办法]
你是不是把form1的name给改了啊
[解决办法]
改成这样:
uses
Forms,
untPerson in 'untPerson.pas',
untBoss in 'untBoss.pas',
untEmployee in 'untEmployee.pas',
untPieceWork in 'untPieceWork.pas',
untHourlyWorker in 'untHourlyWorker.pas',
Unit1 in 'Unit1.pas' {Form1};

读书人网 >.NET

热点推荐